zoukankan      html  css  js  c++  java
  • git 基础学习笔记

    1. 配置ssh

      • 打开命令行或者git bash
      • 输入
    2. 创建git库

      git init
      
    • 检查当前状态
      $ git status
      On branch master
      nothing to commit, working directory clean
      

    working directory clean表示当前工作目录没有未被追踪或者已修改的文件,否则会将它们列出。这个命令会告诉我们当前在哪个分支。
    在当前目录会生成一个.git文件夹

    1. 远程仓库
    • 添加远程仓库
      在GitHub或者码云上创建好仓库后,复制远程仓库的HTTPS地址或ssh地址,在本地文件夹输入:

      git remote add origin <HTTPS地址或ssh地址>
      

      查看远程库信息:

      git remote -v
      
    • 从远程仓库克隆

      git clone <HTTPS地址或ssh地址>
      
    • 推送修改到远程仓库

      • 暂存已修改文件:
        git  add <./特定文件名> 
        

      . 表示当前目录,会将当前目录下修改的所有文件暂存起来;特定文件名则表示将特定文件暂存。

      • 检查已暂存以及尚未暂存的更改
        git diff
        

      该命令会对比目前工作目录(working directory)与暂存区域(stage area)的版本,然后显示尚未暂存的文件的变更

        git diff --cached
      

      上一个命令如果已经暂存了所有更改的话,就什么都不显示。而该命令可以显示已暂存的更改与最后一次commit的差异。

    • 将远程修改加载到本地仓库

      git commit -m <提交信息>
      
      • 跳过暂存环节,直接提交
        git commit -a -m <提交信息>
        
    1. 分支管理
    • 查看分支
      查看本地分支:

      git branch 
      

      查看所有分支:

      git branch -a
      
    • 创建分支

      git branch <分支名>
      
    • 切换分支

      git checkout <分支名>
      

      创建并切换分支:

      git checkout -b <分支名>
      

      切换远程分支:

      git checkout -b <分支名> origin/<分支名>
      

      上面的命令作用是获取远程的分支,在本地命名并切换。

    • 合并分支
      如果要将dev分支的内容合并到master:

      git checkout master
      git merge dev
      

      上面的命令会丢掉分支信息,可以使用:

      git merge --no-ff dev
      

      它会禁用fast forward,使得日志中显示分支的merge信息。

      合并部分分支:

      git cherry-pick <commit 号>
      

      合并多个commit(用空格指定多个commit):

      git cherry-pick A B C D E F
      

      指定commit范围合并(两次版本间使用..连起来):

      git cherry-pick A..B
      

      这样可以从版本A(不包含)到B(包含)的版本pull到当前分支

      可以多段合并,同样用空格隔开:

      git cherry-pick A..B C..D E..F
      

      合并远程分支:

    • 删除分支

      git branch -d <要删除的分支名>
      

      强制删除:

      git branch -D <要删除的分支名>
      

      删除远程分支:

      git push origin :<branchName>
      
    • 解决冲突
      有时候提交、pull以及合并的时候会出现以下情况:

      $ git merge dev
      Auto-merging readme.md
      CONFLICT (content): Merge conflict in readme.md
      Automatic merge failed; fix conflicts and then commit the result.
      

      上面的信息中会标出冲突文件,打开文件会发现,冲突的地方会分别有<<<<<,======,>>>>>标记出不同分支的内容,需要手动修改这些地方,留下你觉得正确需要的修改,其它删除掉,<<<<<表示的是当前分支的内容,>>>>>表示的是要合并过来的分支的内容。
      修改完后在提交就可以了。

    • stash 操作
      stash操作可以将当前尚不需要暂存的修改隐藏起来:

      $ git stash
      Saved working directory and index state WIP on dev: cb5a443 modify the file
      HEAD is now at cb5a443 modify the file
      $ git status
      On branch dev
      nothing to commit, working directory clean
      

      列出隐藏起来的工作现场:

      $ git stash list
      stash@{0}: WIP on dev: cb5a443 modify the file
      

      恢复隐藏的工作现场

      $ git stash pop
      On branch dev
      Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
      
              modified:   readme.md
      
      no changes added to commit (use "git add" and/or "git commit -a")
      Dropped refs/stash@{0} (5e674302787482fbdd0c3e6fb455a5aca102a9d2)
      

      pop命令在恢复的同时会将栈顶的stash的内容删除,调用git stash apply恢复后,stash内容并不会删除,需要调用git stash drop命令才能删除
      多次stash的话,可以先调用git stash list查看stash列表,然后调用git stash apply stash@{

    • 推送修改到远程分支

      git add .
      git commit -m "..."
      git push origin <branchName>
      
    • 获取远程修改的几种方式

      git pull
      

      这个操作包含了fetch和merge,会创建多个临时分支,不是很好,可以这样:

      git pull --rebase
      

      当出现冲突时会:

      $ git pull --rebase origin dev
      From https://github.com/liberty2015/gitdemo
      * branch            dev        -> FETCH_HEAD
      First, rewinding head to replay your work on top of it...
      Applying: update the file
      Using index info to reconstruct a base tree...
      M       readme.md
      Falling back to patching base and 3-way merge...
      Auto-merging readme.md
      CONFLICT (content): Merge conflict in readme.md
      error: Failed to merge in the changes.
      Patch failed at 0001 update the file
      The copy of the patch that failed is found in: .git/rebase-apply/patch
      
      When you have resolved this problem, run "git rebase --continue".
      If you prefer to skip this patch, run "git rebase --skip" instead.
      To check out the original branch and stop rebasing, run "git rebase --abort".
      

      当解决完冲突后:

      git rebase --continue
      

      或者跳过:

      git rebase --skip
      
    1. 标签管理

    2. 日志查询

      git log -<num>
      

      查询日志,num表示查询日志数量
      图形工具查询方式:

      gitk
      

    参考网址:
    https://ihower.tw/blog/archives/3843
    https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001375840202368c74be33fbd884e71b570f2cc3c0d1dcf000
    http://hungyuhei.github.io/2012/08/07/better-git-commit-graph-using-pull---rebase-and-merge---no-ff.html
    https://kingofamani.gitbooks.io/git-teach/content/chapter_2/repo.html

  • 相关阅读:
    [LeetCode] Max Increase to Keep City Skyline 保持城市天际线的最大增高
    [LeetCode] Bricks Falling When Hit 碰撞时砖头掉落
    [LeetCode] Number of Lines To Write String 写字符串需要的行数
    [LeetCode] Unique Morse Code Words 独特的摩斯码单词
    [LeetCode] Find Eventual Safe States 找到最终的安全状态
    [LeetCode] Minimum Swaps To Make Sequences Increasing 使得序列递增的最小交换
    [LeetCode] Similar RGB Color 相似的红绿蓝颜色
    [LeetCode] Champagne Tower 香槟塔
    [LeetCode] Smallest Rotation with Highest Score 得到最高分的最小旋转
    [LeetCode] All Paths From Source to Target 从起点到目标点到所有路径
  • 原文地址:https://www.cnblogs.com/libertycode/p/8327638.html
Copyright © 2011-2022 走看看