在 git 中,fetch 与 pull 有何不同,merge 与 rebase 有何不同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14894768/
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 15:31:27  来源:igfitidea点击:

In git how is fetch different than pull and how is merge different than rebase?

git

提问by techsjs2013

I just cant understand this. I been reading a lot on the web and books and something is just not staying in my head. Can someone please give me the dummy version of the following:

我就是无法理解这一点。我在网上和书籍上阅读了很多东西,但有些东西并没有留在我的脑海中。有人可以给我以下的虚拟版本:

  • git fetch vs pull
  • git merge vs rebase
  • git fetch 与 pull
  • git 合并 vs 变基

回答by pestrella

fetch vs pull

取与拉

fetchwill download any changes from the remote* branch, updating your repository data, but leaving your local* branch unchanged.

fetch将从远程* 分支下载任何更改,更新您的存储库数据,但保持本地* 分支不变。

pullwill perform a fetchand additionally mergethe changes into your local branch.

pull将对您的本地分支执行一个fetch和另外merge的更改。

What's the difference?pullupdates you local branch with changes from the pulled branch. A fetchdoes not advance your local branch.

有什么不同?pull使用拉取分支的更改更新您的本地分支。Afetch不推进您当地的分支机构。

merge vs rebase

合并与变基

Given the following history:

鉴于以下历史:

          C---D---E local
         /
    A---B---F---G remote

mergejoins two development histories together. It does this by replaying the changes that occurred on your local branch after it diverged on top of the remote branch, and record the result in a new commit. This operation preserves the ancestry of each commit.

merge将两个发展历史连接在一起。它通过重播本地分支在远程分支上分叉后发生的更改来实现这一点,并将结果记录在新的提交中。此操作保留每个提交的祖先。

The effect of a mergewill be:

a 的效果merge是:

          C---D---E local
         /         \
    A---B---F---G---H remote

rebasewill take commits that exist in your local branch and re-apply them on top of the remote branch. This operation re-writes the ancestors of your local commits.

rebase将获取本地分支中存在的提交,并将它们重新应用到远程分支之上。此操作会重写本地提交的祖先。

The effect of a rebasewill be:

a 的效果rebase是:

                  C'--D'--E' local
                 /
    A---B---F---G remote

What's the difference?A mergedoes not change the ancestry of commits. A rebaserewrites the ancestry of your local commits.

有什么不同?Amerge不会改变提交的祖先。Arebase重写了本地提交的祖先。

*This explanation assumes that the current branch is a local branch, and that the branch specified as the argument to fetch, pull, merge, or rebaseis a remote branch. This is the usual case. pull, for example, will download any changes from the specifiedbranch, update your repository and mergethe changes into the currentbranch.

*本说明假定当前分支是本地分支,该分支指定为参数fetchpullmerge,或rebase是一个远程分支。这是通常的情况。pull例如,将从指定分支下载任何更改,将您的存储库和merge更改更新到当前分支。

回答by Felipe Sabino

Fetch vs Pull

获取 vs 拉取

Git fetch just updates your repo data, but a git pull will basically perform a fetch and then merge the branch pulled

Git fetch 只是更新你的 repo 数据,但是 git pull 基本上会执行一个 fetch 然后合并拉取的分支

What is the difference between 'git pull' and 'git fetch'?

“git pull”和“git fetch”有什么区别?



Merge vs Rebase

合并与变基

from Atlassian SourceTree Blog, Merge or Rebase:

来自 Atlassian SourceTree 博客,合并或变基

Merging brings two lines of development together while preserving the ancestry of each commit history.

In contrast, rebasing unifies the lines of development by re-writing changes from the source branch so that they appear as children of the destination branch – effectively pretending that those commits were written on top of the destination branch all along.

合并将两条开发线结合在一起,同时保留每个提交历史的祖先。

相比之下,变基通过重写源分支的更改来统一开发线,使它们显示为目标分支的子项——有效地假装这些提交一直写在目标分支的顶部。

Also, check out Learn Git Branching, which is a nice game that has just been posted to HackerNews (link to post) and teaches a lot of branching and merging tricks. I believe it will be very helpful in this matter.

此外,请查看Learn Git Branching,这是一款刚刚发布到 HackerNews(帖子链接)的好游戏,它教授了许多分支和合并技巧。我相信这对这件事会很有帮助。

回答by Steinar

pull vs fetch:

拉与取

The way I understand this, is that git pullis simply a git fetchfollowed by git merge. I.e. you fetch the changes from a remote branch and then merge it into the current branch.

我理解这一点的方式是,git pull它只是一个git fetch后跟git merge. 即您从远程分支获取更改,然后将其合并到当前分支中。



merge vs rebase:

合并与变基

A merge will do as the command says; merge the differences between current branch and the specified branch (into the current branch). I.e. the command git merge another_branchwill the merge another_branchinto the current branch.

合并将按照命令进行;合并当前分支和指定分支之间的差异(进入当前分支)。即命令git merge another_branch将合并another_branch到当前分支。

A rebase works a bit differently and is kind of cool. Let's say you perform the command git rebase another_branch. Git will first find the latest common version between the current branch and another_branch. I.e. the point before the branches diverged. Then git will move this divergent point to the head of the another_branch. Finally, all the commits in the current branch since the original divergent point are replayedfrom the new divergent point. This creates a very clean history, with fewer branches and merges.

rebase 的工作方式有点不同,而且很酷。假设您执行命令git rebase another_branch。Git 会首先找到当前分支和another_branch. 即分支分叉之前的点。然后 git 会将这个发散点移动到another_branch. 最后,从新的发散点重放自原始发散点以来当前分支中的所有提交。这创建了一个非常干净的历史,具有更少的分支和合并。

However, it is not without pitfalls! Since the version history is "rewritten", you should only do this if the commits only exists in your local git repo. That is: Neverdo this if you have pushed the commits to a remote repo.

然而,它并非没有陷阱!由于版本历史已被“重写”,因此只有在提交仅存在于本地 git 存储库中时才应执行此操作。那就是:千万不要,如果你有推提交到远程回购做到这一点。

The explanation on rebasing given in thisonline book is quite good, with easy-to-understand illustrations.

在线书籍中对rebase的解释非常好,并带有易于理解的插图。



pull with rebasing instead of merge

拉动变基而不是合并

I'm actually using rebase quite a lot, but usually it is in combination with pull:

我实际上使用了很多 rebase,但通常它与 pull 结合使用:

git pull --rebase

will fetch remote changes and then rebase instead of merge. I.e. it will replay all your local commits from the last time you performed a pull. I find this much cleaner than doing a normal pull with merging, which will create an extra commit with the merges.

将获取远程更改,然后变基而不是合并。即它将重播上次执行拉取时的所有本地提交。我发现这比通过合并进行普通拉取要干净得多,这将创建一个额外的合并提交。

回答by Nesha Zoric

Merge- HEAD branch will generate a new commit, preserving the ancestry of each commit history. History can become polluted if merge commits are made by multiple people who work on the same branch in parallel.

合并- HEAD 分支将生成一个新的提交,保留每个提交历史的祖先。如果合并提交是由在同一分支上并行工作的多个人进行的,则历史可能会受到污染。

Rebase- Re-writes the changes of one branch onto another without creating a new commit. The code history is simplified, linear and readable but it doesn't work with pull requests, because you can't see what minor changes someone made.

Rebase- 将一个分支的更改重新写入另一个分支,而无需创建新的提交。代码历史经过简化、线性和可读,但它不适用于拉取请求,因为您看不到某人所做的微小更改。

I would use git mergewhen dealing with feature-based workflow or if I am not familiar with rebase. But, if I want a more a clean, linear history then git rebaseis more appropriate. For more details be sure to check out this merge or rebase article.

我会git merge在处理基于特征的工作流程时使用,或者如果我不熟悉 rebase。但是,如果我想要一个更干净、线性的历史,那么git rebase更合适。有关更多详细信息,请务必查看此合并或 rebase 文章