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

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

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

gitversion-controlgit-pullgit-fetch

提问by pupeno

What are the differences between git pulland git fetch?

git pull和之间有什么区别git fetch

采纳答案by Greg Hewgill

In the simplest terms, git pulldoes a git fetchfollowed by a git merge.

用最简单的术语来说,就是git pullagit fetch后跟 a git merge

You can do a git fetchat any time to update your remote-tracking branches under refs/remotes/<remote>/.

你可以git fetch随时更新你的远程跟踪分支refs/remotes/<remote>/

This operation never changes any of your own local branches under refs/heads, and is safe to do without changing your working copy. I have even heard of people running git fetchperiodically in a cron job in the background (although I wouldn't recommend doing this).

此操作永远不会更改您在 下的任何本地分支refs/heads,并且无需更改您的工作副本即可安全执行。我什至听说有人git fetch在后台定期运行cron 作业(尽管我不建议这样做)。

A git pullis what you would do to bring a local branch up-to-date with its remote version, while also updating your other remote-tracking branches.

Agit pull是您将本地分支的远程版本更新为最新版本,同时更新其他远程跟踪分支的操作​​。

Git documentation – git pull:

Git 文档 – git pull

In its default mode, git pullis shorthand for git fetchfollowed by git merge FETCH_HEAD.

在其默认模式下,git pullgit fetch后跟 的简写git merge FETCH_HEAD

回答by Mouna Cheikhna

  • When you use pull, Git tries to automatically do your work for you. It is context sensitive, so Git will merge any pulled commits into the branch you are currently working in. pullautomatically merges the commits without letting you review them first. If you don't closely manage your branches, you may run into frequent conflicts.

  • When you fetch, Git gathers any commits from the target branch that do not exist in your current branch and stores them in your local repository. However, it does not merge them with your current branch. This is particularly useful if you need to keep your repository up to date, but are working on something that might break if you update your files. To integrate the commits into your master branch, you use merge.

  • 当您使用 时pull,Git 会尝试自动为您完成工作。它是上下文相关的,因此 Git 会将所有拉取的提交合并到您当前工作的分支中。 pull自动合并提交,而无需您先查看它们。如果您不密切管理您的分支机构,您可能会遇到频繁的冲突。

  • 当您使用时fetch,Git 从目标分支收集当前分支中不存在的任何提交,并将它们存储在您的本地存储库中。但是,它不会将它们与您当前的分支合并。如果您需要使存储库保持最新状态,但正在处理更新文件可能会中断的事情,这将特别有用。要将提交集成到您的主分支中,您可以使用merge.

回答by MikeD

It is important to contrast the design philosophy of git with the philosophy of a more traditional source control tool like SVN.

将 git 的设计理念与更传统的源代码控制工具(如 SVN)的理念进行对比非常重要。

Subversion was designed and built with a client/server model. There is a single repository that is the server, and several clients can fetch code from the server, work on it, then commit it back to the server. The assumption is that the client can always contact the server when it needs to perform an operation.

Subversion 是使用客户端/服务器模型设计和构建的。有一个作为服务器的存储库,多个客户端可以从服务器获取代码,对其进行处理,然后将其提交回服务器。假设是客户端在需要执行操作时始终可以联系服务器。

Git was designed to support a more distributed model with no need for a central repository (though you can certainly use one if you like). Also git was designed so that the client and the "server" don't need to be online at the same time. Git was designed so that people on an unreliable link could exchange code via email, even. It is possible to work completely disconnected and burn a CD to exchange code via git.

Git 旨在支持更分布式的模型,无需中央存储库(尽管您当然可以根据需要使用一个)。git 的设计也使得客户端和“服务器”不需要同时在线。Git 的设计目的是让处于不可靠链接的人们甚至可以通过电子邮件交换代码。可以完全断开连接并刻录 CD 以通过 git 交换代码。

In order to support this model git maintains a local repository with your code and also an additional local repository that mirrors the state of the remote repository. By keeping a copy of the remote repository locally, git can figure out the changes needed even when the remote repository is not reachable. Later when you need to send the changes to someone else, git can transfer them as a set of changes from a point in time known to the remote repository.

为了支持这个模型,git 维护了一个包含你的代码的本地存储库和一个额外的本地存储库,它反映了远程存储库的状态。通过在本地保留远程存储库的副本,即使远程存储库不可访问,git 也可以计算出所需的更改。稍后当您需要将更改发送给其他人时,git 可以将它们作为一组更改从远程存储库已知的时间点传输。

  • git fetchis the command that says "bring my local copy of the remote repository up to date."

  • git pullsays "bring the changes in the remote repository to where I keep my own code."

  • git fetch是“更新我的远程存储库的本地副本”的命令。

  • git pull说“将远程存储库中的更改带到我保存自己代码的地方。”

Normally git pulldoes this by doing a git fetchto bring the local copy of the remote repository up to date, and then merging the changes into your own code repository and possibly your working copy.

通常git pull这样做是通过执行 agit fetch使远程存储库的本地副本保持最新,然后将更改合并到您自己的代码存储库和可能的工作副本中。

The take away is to keep in mind that there are often at least three copiesof a project on your workstation. One copy is your own repository with your own commit history. The second copy is your working copy where you are editing and building. The third copy is your local "cached" copy of a remote repository.

需要注意的是,您的工作站上通常至少有三个项目副本。一份副本是您自己的存储库,具有您自己的提交历史记录。第二个副本是您正在编辑和构建的工作副本。第三个副本是远程存储库的本地“缓存”副本。

回答by Contango

Here is Oliver Steele's image of how all it all fits together:

这是奥利弗·斯蒂尔( Oliver Steele) 的图片,描述了这一切是如何组合在一起的

enter image description here

在此处输入图片说明

If there is sufficient interest, I suppose I could update the image to add git cloneand git merge...

如果有足够的兴趣,我想我可以更新图像以添加git clonegit merge...

回答by mepster

One use case of git fetchis that the following will tell you any changes in the remote branch since your last pull... so you can check before doing an actual pull, which could change files in your current branch and working copy.

的一个用例git fetch是,以下内容将告诉您自上次拉取以来远程分支中的任何更改......因此您可以在进行实际拉取之前进行检查,这可能会更改当前分支和工作副本中的文件。

git fetch
git diff ...origin

See: https://git-scm.com/docs/git-diffregarding double- and triple-dot syntax in the diff command

请参阅:https: //git-scm.com/docs/git-diff关于 diff 命令中的双点和三点语法

回答by Gerardo

It cost me a little bit to understand what was the difference, but this is a simple explanation. masterin your localhost is a branch.

我花了一点时间才明白有什么区别,但这是一个简单的解释。master在您的本地主机中是一个分支。

When you clone a repository you fetch the entire repository to you local host. This means that at that time you have an origin/master pointer to HEADand master pointing to the same HEAD.

当您克隆存储库时,您将整个存储库提取到本地主机。这意味着那时您有一个HEAD指向同一个HEAD.

when you start working and do commits you advance the master pointer to HEAD+ your commits. But the origin/master pointer is still pointing to what it was when you cloned.

当您开始工作并提交时,您将主指针推进到HEAD+ 您的提交。但是原点/主指针仍然指向克隆时的内容。

So the difference will be:

所以区别将是:

  • If you do a git fetchit will just fetch all the changes in the remote repository (GitHub) and move the origin/master pointer to HEAD. Meanwhile your local branch master will keep pointing to where it has.
  • If you do a git pull, it will do basically fetch (as explained previously) and merge any new changes to your master branch and move the pointer to HEAD.
  • 如果你这样做,git fetch它只会获取远程存储库 ( GitHub)中的所有更改并将 origin/master 指针移动到HEAD. 同时,您的本地分支 master 将继续指向它所在的位置。
  • 如果您执行 a git pull,它基本上会执行 fetch (如前所述)并将任何新更改合并到您的主分支并将指针移动到HEAD

回答by thedarkpassenger

Sometimes a visual representation helps.

有时,视觉表现会有所帮助。

enter image description here

在此处输入图片说明

回答by Snowcrash

Briefly

简要地

git fetchis similar to pullbut doesn't merge. i.e. it fetches remote updates (refsand objects) but your local stays the same (i.e. origin/mastergets updated but masterstays the same) .

git fetch类似于pull但不合并。即它获取远程更新(refsobjects)但你的本地保持不变(即origin/master得到更新但master保持不变)。

git pullpulls down from a remote and instantly merges.

git pull从遥控器下拉并立即合并。

More

更多的

git cloneclones a repo.

git clone克隆一个 repo。

git rebasesaves stuff from your current branch that isn't in the upstream branch to a temporary area. Your branch is now the same as before you started your changes. So, git pull -rebasewill pull down the remote changes, rewind your local branch, replay your changes over the top of your current branch one by one until you're up-to-date.

git rebase将当前分支中不在上游分支中的内容保存到临时区域。您的分支现在与开始更改之前相同。因此,git pull -rebase将拉下远程更改,回退您的本地分支,在当前分支的顶部一一重放您的更改,直到您保持最新状态。

Also, git branch -awill show you exactly what's going on with all your branches - local and remote.

此外,git branch -a将向您展示所有分支机构(本地和远程)的确切情况。

This blog post was useful:

这篇博文很有用:

The difference between git pull, git fetch and git clone (and git rebase) - Mike Pearce

git pull、git fetch 和 git clone(和 git rebase)之间的区别 - Mike Pearce

and covers git pull, git fetch, git cloneand git rebase.

并涵盖git pullgit fetchgit clonegit rebase

====

====

UPDATE

更新

I thought I'd update this to show how you'd actually use this in practice.

我想我会更新这个来展示你在实践中如何实际使用它。

  1. Update your local repo from the remote (but don't merge):

    git fetch 
    
  2. After downloading the updates, let's see the differences:

    git diff master origin/master 
    
  3. If you're happy with those updates, then merge:

    git pull
    
  1. 从远程更新您的本地存储库(但不要合并):

    git fetch 
    
  2. 下载更新后,让我们看看差异:

    git diff master origin/master 
    
  3. 如果您对这些更新感到满意,请合并:

    git pull
    

Notes:

笔记:

On step 2: For more on diffs between local and remotes, see: How to compare a local git branch with its remote branch?

在第 2 步:有关本地和远程之间差异的更多信息,请参阅:如何将本地 git 分支与其远程分支进行比较?

On step 3: It's probably more accurate (e.g. on a fast changing repo) to do a git rebase originhere. See @Justin Ohms commentin another answer.

在第 3 步:git rebase origin在这里做一个可能更准确(例如在快速变化的回购中)。请参阅另一个答案中的@Justin Ohms 评论

See also: http://longair.net/blog/2009/04/16/git-fetch-and-merge/

另见:http: //longair.net/blog/2009/04/16/git-fetch-and-merge/

回答by Vinko Vrsalovic

git-pull - Fetch from and merge with another repository or a local branch
SYNOPSIS

git pull   …
DESCRIPTION

Runs git-fetch with the given parameters, and calls git-merge to merge the 
retrieved head(s) into the current branch. With --rebase, calls git-rebase 
instead of git-merge.

Note that you can use . (current directory) as the <repository> to pull 
from the local repository — this is useful when merging local branches 
into the current branch.

Also note that options meant for git-pull itself and underlying git-merge 
must be given before the options meant for git-fetch.

You would pull if you want the histories merged, you'd fetch if you just 'want the codez' as some person has been tagging some articles around here.

如果你想要合并历史,你会拉,如果你只是“想要代码”,你会获取,因为有人在这里标记了一些文章。

回答by Antonio Bardazzi

You can fetch from a remote repository, see the differences and then pull or merge.

您可以从远程存储库中获取,查看差异,然后拉取或合并。

This is an example for a remote repository called originand a branch called mastertracking the remote branch origin/master:

这是一个名为远程存储库origin和一个名为master跟踪远程分支的分支的示例origin/master

git checkout master                                                  
git fetch                                        
git diff origin/master
git rebase origin master