git pull VS git fetch VS git rebase

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3357122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:50:04  来源:igfitidea点击:

git pull VS git fetch Vs git rebase

gitgit-rebasegit-pullgit-fetch

提问by michael

Another questionsaid git pullis like a git fetch+ git merge.

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

But what is the difference between git pullVS git fetch+ git rebase?

但是git pullVS git fetch+之间有什么区别git rebase

回答by Cascabel

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

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

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:

因此,让我们假设您处于常见情况 - 您已经在 master 分支上完成了一些工作,并且您从 origin 中拉取了一些工作,这也做了一些工作。获取之后,事情看起来像这样:

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

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

如果你此时合并(git pull 的默认行为),假设没有任何冲突,你最终会得到这个:

- 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)

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.

在这两种情况下,您的工作树的内容应该是相同的;你刚刚创造了一个不同的历史导致它。rebase 重写了你的历史,使它看起来好像你是在 origin 的新主分支 ( R)之上提交的,而不是你最初提交的地方 ( H)。如果其他人已经从你的 master 分支中拉取了资源,你永远不应该使用 rebase 方法。

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

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

回答by harshvchawla

TLDR:

域名注册地址:

git pullis like running git fetchthen git merge
git pull --rebaseis like git fetchthen git rebase

git pull就像跑步 git fetch然后git merge
git pull --rebase就像git fetch那时git rebase

In reply to your first statement,

回复你的第一句话,

git pullis like a git fetch+ git merge.

git pull就像一个git fetch+ git merge

"In its default mode, git pull is shorthand for git fetchfollowed by git mergeFETCH_HEAD" More precisely, git pullruns git fetchwith the given parameters and then calls git mergeto merge the retrieved branch heads into the current branch"

“在默认模式下,git pull 是git fetch后跟 git mergeFETCH_HEAD 的简写”更准确地说,使用给定的参数git pull运行git fetch,然后调用git merge将检索到的分支头合并到当前分支中”

(Ref: https://git-scm.com/docs/git-pull)

(参考:https: //git-scm.com/docs/git-pull



For your second statement/question:

对于您的第二个陈述/问题:

'But what is the difference between git pullVS git fetch+ git rebase'

'但是git pullVS git fetch+ 有什么区别git rebase'

Again, from same source:
git pull --rebase

同样,来自同一来源:
git pull --rebase

"With --rebase, it runs git rebase instead of git merge."

“使用 --rebase,它运行 git rebase 而不是 git merge。”



Now, if you wanted to ask

现在,如果你想问

'the difference between mergeand rebase'

'mergerebase'的区别

that is answered here too:
https://git-scm.com/book/en/v2/Git-Branching-Rebasing
(the difference between altering the way version history is recorded)

这也在这里回答:
https: //git-scm.com/book/en/v2/Git-Branching-Rebasing
(改变版本历史记录方式之间的区别)