git pull和git fetch + git rebase有什么区别?

git pull和git fetch + git rebase有什么区别?

问题描述:

另一个问题说,git pull就像git fetch + git merge.

但是git pullgit fetch + git rebase有什么区别?

But what is the difference between git pull and git fetch + git rebase?

从您的问题中可以明显看出,您实际上只是在询问git mergegit rebase之间的区别.

It should be pretty obvious from your question that you're actually just asking about the difference between git merge and git rebase.

因此,假设您处于一般情况下-您已在master分支上完成了一些工作,并且从origin分支中拉了过来,这也已完成了一些工作.提取后,情况如下:

So let's suppose you're in the common case - you've done some work on your master branch, and you pull from origin's, which also has done some work. After the fetch, things look like this:

- o - o - o - H - A - B - C (master)
               \
                P - Q - R (origin/master)

如果此时合并(git pull的默认行为),则假设没有任何冲突,您将得到以下结果:

If you merge at this point (the default behavior of git pull), assuming there aren't any conflicts, you end up with this:

- o - o - o - H - A - B - C - X (master)
               \             /
                P - Q - R --- (origin/master)

另一方面,如果您进行了适当的变基,则最终结果是:

If on the other hand you did the appropriate rebase, you'd end up with this:

- o - o - o - H - P - Q - R - A' - B' - C' (master)
                          |
                          (origin/master)

在两种情况下,工作树的内容都应该相同.您刚刚创建了一条不同的历史记录.重设基础将重写您的历史记录,使其看起来好像您已在原始的新主分支(R)上提交,而不是最初提交的位置(H)一样.如果其他人已经从您的master分支中撤出,则永远不要使用rebase方法.

The content of your work tree should end up the same in both cases; you've just created a different history leading up to it. The rebase rewrites your history, making it look as if you had committed on top of origin's new master branch (R), instead of where you originally committed (H). You should never use the rebase approach if someone else has already pulled from your master branch.

最后,请注意,您实际上可以通过将config参数branch.<name>.rebase设置为true来为给定分支设置git pull以使用rebase而不是合并.您也可以使用git pull --rebase一次拉动.

Finally, note that you can actually set up git pull for a given branch to use rebase instead of merge by setting the config parameter branch.<name>.rebase to true. You can also do this for a single pull using git pull --rebase.