团队作战时,如何让提交记录更干净,让后期做版本合并的同学更省力?
只需要一个简单的原则:
git pull –rebase
详解如下:
开发过程中,为了提交review,需要不时地commit当前的修改;为了跑测试,需要不时地把服务端的代码pull到本地。在这种情况下,如果一个feature开发持续一两周,就会形成这样一个commit log:
HEAD <– My bugfix <– merge log <– team code … <– My bugfix … <– My submit <– merge log <– team code … <– My init sumit
非常丑!如果用好了rebase,则最终的commit log是这样子:
HEAD <– My bugfix <– My bugfix … <– My submit <– <– My init sumit <– team code … <– team code ..
所有的本地提交log都集中在一起。
为了达成这样的效果,你只需要每次pull都执行:
git pull –rebase
git pull –rebase
git pull –rebase
git pull –rebase
git pull –rebase
git pull –rebase
放心执行吧,不会出什么鬼。
update @2017.2.22
git rebase -i 交互式更改commit消息
git commit –amend 提交和上一条合并
撤销amend的方法:
What you need to do is to create a new commit with the same details as the current HEAD commit, but with the parent as the previous version of HEAD. git reset --soft will move the branch pointer so that the next commit happens on top of a different commit from where the current branch head is now.
# Move the current head so that it's pointing at the old commit
# Leave the index intact for redoing the commit.
# HEAD@{1} gives you "the commit that HEAD pointed at before
# it was moved to where it currently points at". Note that this is
# different from HEAD~1, which gives you "the commit that is the
# parent node of the commit that HEAD is currently pointing to."
git reset --soft HEAD@{1}
# commit the current tree using the commit details of the previous
# HEAD commit. (Note that HEAD@{1} is pointing somewhere different from the
# previous command. It's now pointing at the erroneously amended commit.)
git commit -C HEAD@{1}
reference:
https://www.atlassian.com/git/tutorials/merging-vs-rebasing/
http://stackoverflow.com/questions/1459150/how-to-undo-git-commit-amend-done-instead-of-git-commit