十分钟捋完 GIT 命令(补充)

GIT命令补充

之前写了十分钟捋完 GIT 命令,内容包括了一些常用且重要的命令。但是不够完整,今天再补充一些命令。

简单命令

说明:首先把一些简单的展示命令罗列一下,这些命令基本都是看一遍就会的。

  • git remote:管理远端仓库信息,以及路径。增加git remote add、删除git remote remove、修改git remote set-xxx
  • git branch:管理分支。查看git branch -av、删除git branch -D、修改git branch -m、关联远程分支git branch --set-upstream
  • git tag:管理标签。查看git tag -l、新增git tag <tagname>、覆盖git tag -f <tagname>、删除git tag -d
  • git status:展示工作区和缓存区的信息,哪些需要add,哪些需要commit。
  • git diff:展示两个commit版本的差异。git diff <commitId>git diff <commitId1> <commitId2>git diff HEAD HEAD~
  • git log:展示文件提交历史。git loggit log -p <file>git log --oneline
  • git blame:展示文件内容的历史。git blame <filename>

revert

说明:回退commit节点的修改内容,并生成一个新的commit节点。

常见用法:

  • git revert <commitId>:回退到某个commit节点。
  • git revert HEAD~n:回退n步。

示例:

# 创建环境
git checkout master && rm * && touch 1111 && git add . && git commit -m "step1" && git push
# 做两个commit,新增2222文件、新增3333文件
touch 2222 && git add . && git commit -m "step2" && touch 3333 && git add . && git commit -m "step3" && git log -3
# 回滚 step2,可以看到2222文件消失、3333保留、多了一个commit历史"revert step2"
git revert HEAD~ && git log -4
# 回滚刚才的回滚记录,可以看到"revert revert step2",文件又变成了1111、2222、3333
git revert HEAD && git log -5
# 重做两个commit,新增4444,内容修改为5555
touch 4444 && touch 4422 && git add . && git commit -m "step4" && echo 5555 >>4444 && git add . && git commit -m "step5"
# 回退step4,4422被删除,4444提示不能还原,要求手动处理
git revert HEAD~

rebase

说明:整理commit历史。可以简化复杂的commit链条,由于会丢失很多commit历史,所以慎用。一般也仅仅在自己的本地开发分支上使用,不要整理别人的commit链条。

常见用法:

  • git rebase -i HEAD~n:合并当前分支的多条commit记录。
  • git rebase <org_branch> <curr_branch>:将当前分支的基点,改为master最新commit。假设两个分支共同祖先为<commitId>,那么当前分支所有的提交先放到.git/rebase下,然后更新<curr_branch><org_branch>版本,最后应用.git/rebase下的提交。
  • git rebase --edit-todo:继续之前的rebase编辑操作(如果你不小心中断了)
  • git rebase --continue:继续下一步

rebase合并commit记录

# 创建环境,4个commit,每个commit对应一个文件
git checkout master && rm * &&\
touch 1111 && git add . && git commit -m "1111" &&\
touch 2222 && git add . && git commit -m "2222" &&\
touch 3333 && git add . && git commit -m "3333" &&\
touch 4444 && git add . && git commit -m "4444" && ls && git log
# 合并commit到最早的1111提交版本
git rebase -i HEAD~4

选择处理commit记录,常用的有

  • p,pick:完全使用这个commit记录。
  • s,squash:使用commit修改,但是合并到前一个commit中。
  • d,drop:不使用commit,丢失修改和日志。

比如下面的操作是最常见的,保留第一个commit,其他全部squash,保留commit修改,但是作为第一个commit的内容。

rebase第一步

git rebase --continue 编辑新的commit消息。

rebase第二步

rebase修改分支基点

# 创建环境,master分支log:step1 step2 step3;testrebase分支log:step1 step1_1
git checkout master && rm * &&\
touch 1111 && git add . && git commit -m "step1" &&\
git checkout -B testrebase && \
echo testrebase >> 1111 && git add . && git commit -m "step1_1" && git checkout master &&\
touch 2222 && git add . && git commit -m "step2" &&\
touch 3333 && git add . && git commit -m "step3"
# 在master分支执行rebase,那么引入step1_1提交记录,重新生成step2 step3的提交记录(因为1111文件被修改了)
git rebase testrebase && cat 1111
# 也可以在testrebase分支上执行rebase,将插入step2 step3的日志,重新生成step1_1(因为新增了2222 3333两个文件)
git rebase master && ls

cherry-pick

说明:将指定的<commitId>应用到当前分支。

常见用法:

  • git cherry-pick <commitId_A> <commitId_B> <commitId_C>:挑选多个commit内容,应用到当前分支。
  • git cherry-pick <commitId_A>..<commitId_N>:挑选a–>n的提交内容,应用到当前分支。不包括A
  • git cherry-pick <commitId_A>~..<commitId_N>:挑选(a-1)–>n的提交内容,应用到当前分支。这下包括A了
  • git cherry-pick --continue:继续未完成的操作。

示例:

# 创造环境,
git checkout master && rm * &&\
touch 1111 && git add . && git commit -m "step11" &&\
git checkout -B cherry1 && echo cherry1 >> 1111 && git add . && git commit -m "cherry11" &&\
git checkout master && touch 2222 && git add . && git commit -m "step22" &&\
git checkout -B cherry2 && echo cherry2 >> 2222 && git add . && git commit -m "cherry22"
# 记录<commitId> b4f8e224  e1033d75
git checkout cherry1 && git log -1
git checkout cherry2 && git log -1
# cherry-pick
git checkout master && git cherry-pick 39124e 255d46
# 验证,在原本的<commitId>后面新增了两个commit历史,同时数据也merge了。
cat 1111 && cat 2222 && git log

#### 假如 master又修改了 1111 2222
echo abcd > 1111 && echo "hello\ncherry2\nhello">2222 && git add . && git commit -m "step33"
# 这一步会提示"error: 不能应用 <commitId>... <commit msg>",
git checkout master && git cherry-pick 39124e 255d46
# 查看哪些文件需要合并
git status
#手动merge后继续
git cherry-pick --continue

提示cherry1
cherry1内容
手动merge后继续
提示cherry2
gitstatus

猜你喜欢

转载自blog.csdn.net/weixin_36572983/article/details/106609959