在开发的时候可能由于频繁的提交信息,比较零散和片断,比如在提交多少次后,想做一个总结。这时候可以把前面几个合并成一个提交信息。
- 进入到cookbook项目,并切换到squash-branch分支。
[email protected] MINGW64 ~/cookbook/cookbook (rebase-branch)
$ git checkout -b squash-branch
Switched to a new branch ‘squash-branch‘
- 在squash-branch分支下创建两个提交 ,并把它两个提交挤压到一起。
[email protected] MINGW64 ~/cookbook/cookbook (squash-branch)
$ echo "1" >> README.md
[email protected] MINGW64 ~/cookbook/cookbook (squash-branch)
$ git add .
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.
[email protected] MINGW64 ~/cookbook/cookbook (squash-branch)
$ git commit -a -m ‘wip1‘
[squash-branch 4e54db8] wip1
1 file changed, 1 insertion(+)
[email protected] MINGW64 ~/cookbook/cookbook (squash-branch)
$ echo "2" >> README.md
[email protected] MINGW64 ~/cookbook/cookbook (squash-branch)
$ git add .
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.
[email protected] MINGW64 ~/cookbook/cookbook (squash-branch)
$ git commit -a -m ‘wip2‘
[squash-branch 5d37a65] wip2
1 file changed, 1 insertion(+)
- 通过以下命令可以查看我们的提交历史。
[email protected] MINGW64 ~/cookbook/cookbook (squash-branch)
$ git log --oneline
5d37a65 (HEAD -> squash-branch) wip2
4e54db8 wip1
f5f7fde (rebase-branch) Another commit
acb491e (master) Commit in master
5e1ebdd (origin/master) Merge branch ‘first-branch‘
dc7b6d5 (first-branch) Readme changed
53ec2ca Added readme file
- 把wip1和wip2的提交信息挤压在一个更好的提交信息中,执行以下命令。此时会打开一个编辑器。
$ git rebase –i HEAD~2
注解: HEAD~2 代表着我们想挤压最后两个提交。假如你想要挤压最后4个提交,那么使用HEAD~4
原始:
改变后:
原文地址:http://blog.51cto.com/13856449/2149566