git pull 和 git fetch 有什么区别? git 变基?

发布于 2024-09-11 11:28:34 字数 305 浏览 1 评论 0原文

另一个问题git pull< /code> 就像 git fetch + git merge 一样。

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

Another question says that git pull is like a git fetch + git merge.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的十字路口 2024-09-18 11:28:34

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

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

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

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

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

如果另一方面你做了适当的变基,你最终结果是这样的:

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

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

最后,请注意,您实际上可以通过将配置参数 branch..rebase 设置为 true 来为给定分支设置 git pull 以使用 rebase 而不是合并。您还可以使用 git pull --rebase 来执行一次拉取操作。

It should be pretty obvious from your question that you're actually just asking about the difference between git merge and 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:

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

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

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.

伴我老 2024-09-18 11:28:34

TLDR:

git pull 就像运行 git fetch 然后 git merge
git pull --rebase 就像 git fetch 然后 git rebase

回复你的第一个语句,

git pull 就像 git fetch + git merge

“在默认模式下,git pull 是 git fetch 的简写,后跟
git merge FETCH_HEAD" 更准确地说,git pull 使用以下命令运行 git fetch
给定参数,然后调用 git merge 来合并检索到的分支
进入当前分支”

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

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

'但是 git pull VS git fetch + git 之间有什么区别rebase'

再次,来自同一来源:
git pull --rebase

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

现在,如果您想问

“合并和rebase之间的区别”

这里也有答案:
https://git-scm.com/book/en/v2/Git -分支-变基
(改变版本历史记录方式的区别)

TLDR:

git pull is like running git fetch then git merge
git pull --rebase is like git fetch then git rebase

In reply to your first statement,

git pull is like a git fetch + git merge.

"In its default mode, git pull is shorthand for git fetch followed by
git merge FETCH_HEAD" More precisely, git pull runs git fetch with the
given parameters and then calls git merge to merge the retrieved branch
heads into the current branch"

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

For your second statement/question:

'But what is the difference between git pull VS git fetch + git rebase'

Again, from same source:
git pull --rebase

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

Now, if you wanted to ask

'the difference between merge and rebase'

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文