好的,您可以在合并后使用 git pull --rebase
命令,它可以完成以下操作:
-r
--rebase
在获取(fetch)后,将当前分支(rebase)在上游(upstream)分支之上。如果有一个对应于上游分支的远程跟踪分支(remote-tracking branch)并且自上次获取(fetch)以来上游分支已经被重新设置(rebased),则该rebase使用该信息来避免rebase非本地更改。
如果您想让git pull始终使用--rebase而不是合并(merge),请参见git-config
(1)中的 pull.rebase, branch.<name>.rebase
和 branch.autosetuprebase
。
注意
这是一种潜在的危险操作模式。它会重写历史记录,如果您已经发布了该历史记录,这样做可能会带来不良影响。请仔细阅读git-rebase
(1),然后再使用此选项。
因此,总结一下:
git branch -b feature
...
git checkout master
git merge feature # By the way, merge's --no-ff might interest you
git pull --rebase # rebases feature onto the new-fetched head if necessary
请注意:如果您对master
进行了更改,则此操作的结果与您的方式略有不同,因为git pull --rebase
将自上次pull/push
以来所有更改都变基到拉取的HEAD
上,而您的方式会导致拉取的更改和您的master
更改合并,然后再将feature
变基于此。这在大多数情况下不重要(个人而言,我更喜欢在我的历史记录中没有不必要的pull
引起的合并),但您仍应该意识到它...
图片示例,之前:
/-O origin/master
C--A--B master
\--F--G feature
在您的方式之后:
/-O-----\
C--A--B--C*-----(FG) (origin/)master - fast-forward merge (unless --no-ff)
\-F-G-/ feature (rebased!)
git pull --rebase
的方式:
/-O
C--A--B--M* => C--O--A--B--M* (origin/)master
\-F-G-/ feature (not rebased!)
如果你希望最终结果是不同的话,可以考虑以下几种方法:
C--O--A--B--F--G
使用
git branch -b feature
...
git checkout master
git rebase feature
git pull --rebase
还要注意,你可以通过使用 git-rebase -i
命令来获得更多的乐趣 ;)
git rebase master feature
。但是请参考我的答案,这是更简单的方法... - Tobias Kienzler