简体   繁体   English

改进git rebase工作流程

[英]Improving a git rebase workflow

I'm currently working a very simple git workflow using feature branches and rebasing on to master before pushing. 我目前正在使用功能分支和在推送之前重新定位到master的非常简单的git工作流程。

git checkout -b feature
.. make some commits
git checkout master
git pull

If there are no changes from the pull: 如果拉动没有变化:

git merge feature
git push

If there are changes: 如果有变化:

git checkout feature
git rebase master
git checkout master
git merge feature
git push

While has been great to learn how git works but it's getting a little tedious to type all the time and I suspect there's some faster ways to achieve what I'm doing but can't find them. 虽然很高兴了解git是如何工作的,但是一直打字有点乏味,我怀疑有一些更快的方法可以实现我正在做的但却找不到它们。

Your shortest amount of steps had a total of 6 steps (no changes case): 您最短的步骤总共有6个步骤(没有更改的情况):

git checkout -b feature
.. make some commits
git checkout master
git pull
git merge feature
git push

This technique should work in both cases (6 steps as well): 这种技术应该适用于两种情况(也包括6个步骤):

git checkout -b feature
.. make some commits
git fetch
git rebase origin/master
git checkout master
git merge feature

Well, you could use git pull --rebase after merging, which does just that: 好吧,你可以合并使用git pull --rebase ,这样做:

-r --rebase -r --rebase

Rebase the current branch on top of the upstream branch after fetching. 在获取后,将当前分支重新定位在上游分支的顶部。 If there is a remote-tracking branch corresponding to the upstream branch and the upstream branch was rebased since last fetched, the rebase uses that information to avoid rebasing non-local changes. 如果存在与上游分支对应的远程跟踪分支,并且自上次提取以来上游分支已重新定位,则rebase使用该信息来避免重新定位非本地更改。

See pull.rebase, branch.<name>.rebase and branch.autosetuprebase in git-config (1) if you want to make git pull always use --rebase instead of merging. 如果你想让git pull总是使用--rebase而不是合并pull.rebase, branch.<name>.rebase请参阅git-config (1)中的 pull.rebase, branch.<name>.rebasebranch.autosetuprebase

Note 注意

This is a potentially dangerous mode of operation. 这是一种潜在危险的操作模式。 It rewrites history, which does not bode well when you published that history already. 它重写了历史,当你已经发布了这段历史时,它并不是一个好兆头。 Do not use this option unless you have read git-rebase (1) carefully. 除非您仔细阅读git-rebase (1),否则请勿使用此选项。

So in summary: 总结如下:

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

Just one note: The result of this is slightly different from your way if you made changes to master as well, since git pull --rebase rebases all your changes since the last pull/push onto the pulled HEAD , while you way would cause the pulled changes and your master changes to be merged and then feature be rebased upon that. 只需注意一下:如果您对master进行了更改,那么结果与您的方式略有不同,因为git pull --rebase自上次pull/push到拉动HEAD以来的所有更改,而您的方式会导致拉出更改并将您的master更改合并,然后根据该feature进行重新设置。 This is mostly irrelevant (and personally I prefer not having unnecessary pull -induced merges in my history) but you should be aware of it nonetheless... 这大多是无关紧要的(我个人并不喜欢在我的历史记录中没有不必要的pull诱导合并),但你应该意识到它......

In pictures, before: 在图片中,之前:

 /-O          origin/master
C--A--B       master
    \--F--G   feature

After your way: 在您的方式之后:

 /-O-----\  
C--A--B--C*-----(FG)  (origin/)master - fast-forward merge (unless --no-ff)
          \-F-G-/     feature (rebased!)

git pull --rebase way: git pull --rebase方式:

 /-O
C--A--B--M*  => C--O--A--B--M*  (origin/)master
   \-F-G-/                      feature (not rebased!)

If instead you want the end result to be 相反,如果你想要最终结果

C--O--A--B--F--G

use 使用

git branch -b feature
...
git checkout master
git rebase feature
git pull --rebase

Also note how much fun you can have with git-rebase -i ;) 还要注意git-rebase -i可以带来多少乐趣;)

probably a nice alias could improve your workflow a little bit, I use this one 可能一个好的别名可以改善你的工作流程,我用这个

# full rebase
frb = "!sh -c 'x=`git rev-parse --abbrev-ref HEAD` && echo "rebasing to $0 ..." && git rebase $0 && git checkout $0 && git rebase $x'"

usage (on feature branch): 用法(在功能分支上):

git frb master

of course you could also add a checkout at the beginning and push at the end 当然,您也可以在开头添加结帐并在结尾处推送

... tested on git 1.9.3 (osx), not tested what happens if there are conflicts ...在git 1.9.3(osx)上测试,未测试如果存在冲突会发生什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM