在工作中,经常会出现有人意外地将某些内容提交到主分支而不是目标特性分支的情况,然后这个人试图解决它,却发现更改突然消失了。我已经四处搜寻,但找不到任何文件可以阐明为什么会发生这种情况,或者如何解决。
以下是重现步骤:
$ git init
$ echo hello world > testfile
$ git add testfile
$ git commit -m 'init'
$ git branch A
$ echo abc >> testfile
$ git commit -am 'append abc'
$ git revert HEAD --no-edit
$ git checkout A
$ echo fff > secondfile
$ git add secondfile
$ git commit -m 'Add second file'
$ git cherry-pick master^
此时 Git 历史记录如下:
$ git log --oneline --all --graph --decorate
* ac6f9b4 (HEAD -> A) append abc
* 54be952 Add second file
| * 9ba1f16 (master) Revert "append abc"
| * ef7c8d6 append abc
|/
* 65a885d init
现在请看当我将分支A变基到主分支上时会发生什么:
$ git rebase master
$ git log --oneline --all --graph --decorate
* 9d08739 (HEAD -> A) Add second file
* 9ba1f16 (master) Revert "append abc"
* ef7c8d6 append abc
* 65a885d init
在A分支的最前端有一次提交,提交号为ac6f9b4,它发生了什么?它去哪里了?为什么没有重新应用?
尽管这只是一个小例子,仅缺少一个提交,但有时我们会丢失长时间提交链中间的多个提交,它们似乎就像隐形一样 :(