简体   繁体   English

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

[英]What is the difference between 'git pull' and 'git fetch'?

What are the differences between git pull and git fetch ? git pullgit fetch有什么区别?

In the simplest terms, git pull does a git fetch followed by a git merge .用最简单的术语来说, git pull执行git fetch后跟git merge


git fetch updates your remote-tracking branches under refs/remotes/<remote>/ . git fetch更新refs/remotes/<remote>/下的远程跟踪分支。 This operation is safe to run at any time since it never changes any of your local branches under refs/heads .此操作可以随时安全运行,因为它永远不会更改refs/heads下的任何本地分支。

git pull brings a local branch up-to-date with its remote version, while also updating your other remote-tracking branches. git pull使用其远程版本更新本地分支,同时更新您的其他远程跟踪分支。

From the Git documentation for git pull :来自git pull的 Git 文档:

In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD .在其默认模式下, git pullgit fetch后跟git merge FETCH_HEAD的简写。

  • git pull tries to automatically merge after fetching commits. git pull尝试在获取提交后自动合并。 It is context sensitive , so all pulled commits will be merged into your currently active branch.它是上下文敏感的,因此所有拉取的提交都将合并到您当前活动的分支中。 git pull automatically merges the commits without letting you review them first . git pull自动合并提交,而不让您先查看它们 If you don't carefully manage your branches, you may run into frequent conflicts.如果你不仔细管理你的分支,你可能会遇到频繁的冲突。

  • git fetch gathers any commits from the target branch that do not exist in the current branch and stores them in your local repository . git fetch从目标分支收集当前分支中不存在的所有提交,并将它们存储在本地存储库中。 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 current branch, you must use git merge afterwards.要将提交集成到您当前的分支中,您必须在之后使用git merge

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. Subversion 是使用客户端/服务器模型设计和构建的。 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.假设客户端在需要执行操作时始终可以联系服务器。

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). Git 旨在支持更分布式的模型,而不需要中央存储库(尽管您当然可以根据需要使用一个)。 Also git was designed so that the client and the "server" don't need to be online at the same time. git 也被设计成客户端和“服务器”不需要同时在线。 Git was designed so that people on an unreliable link could exchange code via email, even. Git 的设计目的是让不可靠链接上的人们甚至可以通过电子邮件交换代码。 It is possible to work completely disconnected and burn a CD to exchange code via 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.为了支持这个模型,git 使用你的代码维护一个本地存储库,以及一个反映远程存储库状态的附加本地存储库。 By keeping a copy of the remote repository locally, git can figure out the changes needed even when the remote repository is not reachable.通过在本地保存远程存储库的副本,即使远程存储库无法访问,git 也可以找出所需的更改。 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 fetch is the command that says "bring my local copy of the remote repository up to date." git fetch是说“使我的远程存储库的本地副本保持最新”的命令。

  • git pull says "bring the changes in the remote repository to where I keep my own code." git pull说“将远程存储库中的更改带到我保存自己代码的地方。”

Normally git pull does this by doing a git fetch to 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通过执行git fetch来更新远程存储库的本地副本,然后将更改合并到您自己的代码存储库和可能的工作副本中。

The take away is to keep in mind that there are often at least three copies of 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.第三个副本是远程存储库的本地“缓存”副本。

One use case of git fetch is 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 git diff documentation regarding the double- .. and triple-dot ... syntax.请参阅有关双..和三点...语法的git diff文档。

It cost me a little bit to understand what was the difference, but this is a simple explanation.我花了一点时间来理解有什么区别,但这是一个简单的解释。 master in 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 HEAD and master pointing to the same HEAD .这意味着那时您有一个指向HEAD的 origin/master 指针和指向同一个HEAD的 master 。

when you start working and do commits you advance the master pointer to HEAD + your commits.当您开始工作并提交时,您将主指针前进到HEAD + 您的提交。 But the origin/master pointer is still pointing to what it was when you cloned.但是原点/主指针仍然指向克隆时的位置。

So the difference will be:所以区别将是:

  • If you do a git fetch it will just fetch all the changes in the remote repository ( GitHub ) and move the origin/master pointer to HEAD .如果您执行git fetch ,它将仅获取远程存储库 ( GitHub ) 中的所有更改并将 origin/master 指针移动到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 pull ,它将基本上执行 fetch (如前所述)并将任何新更改合并到您的 master 分支并将指针移动到HEAD

Sometimes a visual representation helps.有时,视觉表示会有所帮助。

在此处输入图像描述

Even more briefly更简短的

git fetch fetches updates but does not merge them. git fetch获取更新但不合并它们。

git pull does a git fetch under the hood and then a merge . git pull在后台执行git fetch ,然后执行merge

Briefly简要地

git fetch is similar to pull but doesn't merge. git fetch类似于pull但不合并。 ie it fetches remote updates ( refs and objects ) but your local stays the same (ie origin/master gets updated but master stays the same) .即它获取远程更新( refsobjects ),但您的本地保持不变(即origin/master得到更新,但master保持不变)。

git pull pulls down from a remote and instantly merges. git pull从远程下拉并立即合并。

More更多的

git clone clones a repo. git clone克隆一个 repo。

git rebase saves stuff from your current branch that isn't in the upstream branch to a temporary area. git rebase将当前分支中不在上游分支中的内容保存到临时区域。 Your branch is now the same as before you started your changes.您的分支现在与开始更改之前相同。 So, git pull -rebase will 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 pull -rebase将拉下远程更改,倒回您的本地分支,在当前分支的顶部一一重播您的更改,直到您保持最新状态。

Also, git branch -a will 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 clone and 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

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 (eg on a fast changing repo) to do a git rebase origin here.在第 3 步:在这里做一个git rebase origin可能更准确(例如在一个快速变化的 repo 上)。 See @Justin Ohms comment in another answer.请参阅另一个答案中的@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/

Note also: I've mentioned a merge during a pull however you can configure a pull to use a rebase instead.另请注意:我在pull期间提到了merge ,但是您可以将pull配置为使用rebase

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.如果你想合并历史,你会拉,如果你只是“想要 codez”,你会提取,因为有人在这里标记了一些文章。

OK , here is some information about git pull and git fetch , so you can understand the actual differences... in few simple words, fetch gets the latest data, but not the code changes and not going to mess with your current local branch code, but pull get the code changes and merge it your local branch, read on to get more details about each:好的,这里有一些关于git pullgit fetch的信息,所以你可以了解实际的区别......简单来说, fetch获取最新数据,而不是代码更改并且不会弄乱你当前的本地分支代码,但是取代码更改并将其合并到您的本地分支,请继续阅读以获取有关每个更改的更多详细信息:

git fetch获取

It will download all refs and objects and any new branches to your local Repository...它将所有引用对象以及任何新分支下载到您的本地存储库...

Fetch branches and/or tags (collectively, "refs") from one or more other repositories, along with the objects necessary to complete their histories.从一个或多个其他存储库中获取分支和/或标签(统称为“引用”),以及完成其历史所需的对象。 Remote-tracking branches are updated (see the description of below for ways to control this behavior).远程跟踪分支已更新(有关控制此行为的方法,请参见下面的描述)。

By default, any tag that points into the histories being fetched is also fetched;默认情况下,任何指向正在获取的历史的标签也会被获取; the effect is to fetch tags that point at branches that you are interested in. This default behavior can be changed by using the --tags or --no-tags options or by configuring remote..tagOpt.效果是获取指向您感兴趣的分支的标签。可以通过使用 --tags 或 --no-tags 选项或配置 remote..tagOpt 来更改此默认行为。 By using a refspec that fetches tags explicitly, you can fetch tags that do not point into branches you are interested in as well.通过使用显式获取标签的 refspec,您也可以获取不指向您感兴趣的分支的标签。

git fetch can fetch from either a single named repository or URL or from several repositories at once if is given and there is a remotes. git fetch 可以从单个命名存储库或 URL 或从多个存储库中获取,如果给定并且有一个遥控器。 entry in the configuration file.配置文件中的条目。 (See git-config 1 ). (见 git-config 1 )。

When no remote is specified, by default the origin remote will be used, unless there's an upstream branch configured for the current branch.如果未指定远程,默认情况下将使用原始远程,除非为当前分支配置了上游分支。

The names of refs that are fetched, together with the object names they point at, are written to .git/FETCH_HEAD.获取的 ref 的名称以及它们指向的对象名称被写入 .git/FETCH_HEAD。 This information may be used by scripts or other git commands, such as git-pull.脚本或其他 git 命令(例如 git-pull)可能会使用此信息。


git pull git 拉

It will apply the changes from remote to the current branch in local...它会将远程的更改应用到本地的当前分支...

Incorporates changes from a remote repository into the current branch.将来自远程存储库的更改合并到当前分支中。 In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD.在其默认模式下,git pull 是 git fetch 后跟 git merge FETCH_HEAD 的简写。

More precisely, git pull runs git fetch with the given parameters and calls git merge to merge the retrieved branch heads into the current branch.更准确地说, git pull 使用给定的参数运行 git fetch 并调用 git merge 以将检索到的分支头合并到当前分支中。 With --rebase, it runs git rebase instead of git merge.使用 --rebase,它运行 git rebase 而不是 git merge。

should be the name of a remote repository as passed to git-fetch 1 .应该是传递给 git-fetch 1的远程存储库的名称。 can name an arbitrary remote ref (for example, the name of a tag) or even a collection of refs with corresponding remote-tracking branches (eg, refs/heads/ :refs/remotes/origin/ ), but usually it is the name of a branch in the remote repository.可以命名任意远程引用(例如,标签的名称),甚至是具有相应远程跟踪分支的引用集合(例如,refs/heads/ :refs/remotes/origin/ ),但通常是名称远程存储库中的一个分支。

Default values for and are read from the "remote" and "merge" configuration for the current branch as set by git-branch --track.和 的默认值是从 git-branch --track 设置的当前分支的“远程”和“合并”配置中读取的。


I also create the visual below to show you how git fetch and git pull working together...我还创建了下面的视觉效果,向您展示git fetchgit pull如何一起工作......

git pull 和 git fetch

You can fetch from a remote repository, see the differences and then pull or merge.您可以从远程存储库中获取,查看差异,然后提取或合并。

This is an example for a remote repository called origin and a branch called master tracking the remote branch origin/master :这是一个名为origin的远程存储库和一个名为master的分支跟踪远程分支origin/master的示例:

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

The short and easy answer is that git pull is simply git fetch followed by git merge .简短而简单的答案是git pull只是git fetch后跟git merge

It is very important to note that git pull will automatically merge whether you like it or not .需要注意的是,不管你喜不喜欢git pull都会自动合并。 This could, of course, result in merge conflicts.当然,这可能会导致合并冲突。 Let's say your remote is origin and your branch is master .假设您的遥控器是origin而您的分支是master If you git diff origin/master before pulling, you should have some idea of potential merge conflicts and could prepare your local branch accordingly.如果您在拉取之前git diff origin/master ,您应该对潜在的合并冲突有所了解,并可以相应地准备您的本地分支。

In addition to pulling and pushing, some workflows involve git rebase , such as this one, which I paraphrase from the linked article:除了拉取和推送之外, 一些工作流程还涉及git rebase ,例如这个,我从链接的文章中转述:

git pull origin master
git checkout foo-branch
git rebase master
git push origin foo-branch

If you find yourself in such a situation, you may be tempted to git pull --rebase .如果你发现自己处于这种情况,你可能会想git pull --rebase Unless you really, really know what you are doing, I would advise against that.除非你真的,真的知道你在做什么,否则我建议不要这样做。 This warning is from the man page for git-pull , version 2.3.5 :此警告来自git-pullman页,版本2.3.5

This is a potentially dangerous mode of operation.这是一种潜在的危险操作模式。 It rewrites history, which does not bode well when you published that history already.它改写了历史,当您已经发布了该历史时,这并不是一个好兆头。 Do not use this option unless you have read git-rebase(1) carefully.除非您仔细阅读 git-rebase(1),否则不要使用此选项。

在此处输入图像描述

This interactive graphical representation is very helpful in understanging git: http://ndpsoftware.com/git-cheatsheet.html这种交互式图形表示对于理解 git 非常有帮助:http: //ndpsoftware.com/git-cheatsheet.html

git fetch just "downloads" the changes from the remote to your local repository. git fetch只是将更改从远程“下载”到本地存储库。 git pull downloads the changes and merges them into your current branch. git pull下载更改并将它们合并到您当前的分支中。 "In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD ." “在其默认模式下, git pullgit fetch后跟git merge FETCH_HEAD的简写。”

Bonus:奖金:

In speaking of pull & fetch in the above answers, I would like to share an interesting trick,在谈到上述答案中的 pull & fetch 时,我想分享一个有趣的技巧,

git pull --rebase

This above command is the most useful command in my git life which saved a lots of time.上面这个命令是我 git 生活中最有用的命令,它节省了很多时间。

Before pushing your new commits to server, try this command and it will automatically sync latest server changes (with a fetch + merge) and will place your commit at the top in git log.在将您的新提交推送到服务器之前,请尝试此命令,它会自动同步最新的服务器更改(使用 fetch + 合并)并将您的提交放在 git log 的顶部。 No need to worry about manual pull/merge.无需担心手动拉/合并。

Find details at: http://gitolite.com/git-pull--rebase在以下位置查找详细信息:http: //gitolite.com/git-pull--rebase

I like to have some visual representation of the situation to grasp these things.我喜欢对情况有一些直观的表示来掌握这些东西。 Maybe other developers would like to see it too, so here's my addition.也许其他开发人员也想看到它,所以这是我的补充。 I'm not totally sure that it all is correct, so please comment if you find any mistakes.我不完全确定这一切都是正确的,所以如果您发现任何错误,请发表评论。

                                         LOCAL SYSTEM
                  . =====================================================    
================= . =================  ===================  =============
REMOTE REPOSITORY . REMOTE REPOSITORY  LOCAL REPOSITORY     WORKING COPY
(ORIGIN)          . (CACHED)           
for example,      . mirror of the      
a github repo.    . remote repo
Can also be       .
multiple repo's   .
                  .
                  .
FETCH  *------------------>*
Your local cache of the remote is updated with the origin (or multiple
external sources, that is git's distributed nature)
                  .
PULL   *-------------------------------------------------------->*
changes are merged directly into your local copy. when conflicts occur, 
you are asked for decisions.
                  .
COMMIT            .                             *<---------------*
When coming from, for example, subversion, you might think that a commit
will update the origin. In git, a commit is only done to your local repo.
                  .
PUSH   *<---------------------------------------*
Synchronizes your changes back into the origin.

Some major advantages for having a fetched mirror of the remote are:获取遥控器镜像的一些主要优点是:

  • Performance (scroll through all commits and messages without trying to squeeze it through the network)性能(滚动浏览所有提交和消息,而不试图通过网络挤压它)
  • Feedback about the state of your local repo (for example, I use Atlassian's SourceTree, which will give me a bulb indicating if I'm commits ahead or behind compared to the origin. This information can be updated with a GIT FETCH).关于本地 repo 状态的反馈(例如,我使用 Atlassian 的 SourceTree,它会给我一个灯泡,指示与原点相比我是提前还是落后提交。可以使用 GIT FETCH 更新此信息)。

The Difference between GIT Fetch and GIT Pull can be explained with the following scenario: (Keeping in mind that pictures speak louder than words!, I have provided pictorial representation) GIT FetchGIT Pull之间的区别可以用以下场景来解释:(请记住,图片胜于雄辩!,我提供了图片表示)

Let's take an example that you are working on a project with your team members.让我们举一个例子,您正在与您的团队成员一起处理一个项目。 So there will be one main Branch of the project and all the contributors must fork it to their own local repository and then work on this local branch to modify/Add modules then push back to the main branch.所以项目会有一个主分支,所有贡献者必须将它分叉到他们自己的本地存储库,然后在这个本地分支上修改/添加模块,然后推回主分支。

So, Initial State of the two Branches when you forked the main project on your local repository will be like this- ( A , B and C are Modules already completed of the project)因此,当您在本地存储库上分叉主项目时,两个分支的初始状态将是这样的-( ABC是项目已经完成的模块)

在此处输入图像描述

Now, you have started working on the new module (suppose D ) and when you have completed the D module you want to push it to the main branch, But meanwhile what happens is that one of your teammates has developed new Module E , F and modified C .现在,您已经开始开发新模块(假设D ),当您完成D模块后,您想将其推送到主分支,但与此同时,您的一个队友开发了新的模块EF和修改C .
So now what has happened is that your local repository is lacking behind the original progress of the project and thus pushing of your changes to the main branch can lead to conflict and may cause your Module D to malfunction.所以现在发生的事情是您的本地存储库落后于项目的原始进度,因此将您的更改推送到主分支可能会导致冲突并可能导致您的模块D出现故障。

在此处输入图像描述

To avoid such issues and to work parallel with the original progress of the project there are Two ways:为避免此类问题并与项目的原始进度并行工作,有两种方法:

1. Git Fetch- This will Download all the changes that have been made to the origin/main branch project which are not present in your local branch. 1. Git Fetch -这将下载对 origin/main 分支项目所做的所有更改,这些更改不存在于您的本地分支中。 And will wait for the Git Merge command to apply the changes that have been fetched to your Repository or branch.并将等待 Git Merge 命令将已获取的更改应用到您的存储库或分支。

在此处输入图像描述

So now You can carefully monitor the files before merging it to your repository.所以现在您可以在将文件合并到您的存储库之前仔细监控文件。 And you can also modify D if required because of Modified C .由于修改了C ,您还可以根据需要修改D

在此处输入图像描述

2. Git Pull- This will update your local branch with the origin/main branch ie actually what it does is a combination of Git Fetch and Git merge one after another. 2. Git Pull-这将使用原始/主分支更新您的本地分支,即实际上它所做的是Git Fetch 和Git 一个接一个合并的组合。 But this may Cause Conflicts to occur, so it's recommended to use Git Pull with a clean copy.但这可能会导致发生冲突,因此建议使用带有干净副本的 Git Pull。

在此处输入图像描述

I have struggled with this as well.我也为此苦苦挣扎。 In fact I got here with a google search of exactly the same question.事实上,我通过谷歌搜索完全相同的问题来到这里。 Reading all these answers finally painted a picture in my head and I decided to try to get this down looking at the state of the 2 repositories and 1 sandbox and actions performed over time while watching the version of them.阅读所有这些答案最终在我的脑海中描绘了一幅画面,我决定尝试通过查看 2 个存储库和 1 个沙箱的状态以及在查看它们的版本时随时间执行的操作来了解这一点。 So here is what I came up with.所以这就是我想出的。 Please correct me if I messed up anywhere.如果我在任何地方搞砸了,请纠正我。

The three repos with a fetch:三个带有 fetch 的 repos:

---------------------     -----------------------     -----------------------
- Remote Repo       -     - Remote Repo         -     - Remote Repo         -
-                   -     - gets pushed         -     -                     -
- @ R01             -     - @ R02               -     - @ R02               -
---------------------     -----------------------     -----------------------

---------------------     -----------------------     -----------------------
- Local Repo        -     - Local Repo          -     - Local Repo          -
- pull              -     -                     -     - fetch               -
- @ R01             -     - @ R01               -     - @ R02               -
---------------------     -----------------------     -----------------------

---------------------     -----------------------     -----------------------
- Local Sandbox     -     - Local Sandbox       -     - Local Sandbox       -
- Checkout          -     - new work done       -     -                     -
- @ R01             -     - @ R01+              -     - @R01+               -
---------------------     -----------------------     -----------------------

The three repos with a pull拉动的三个回购

---------------------     -----------------------     -----------------------
- Remote Repo       -     - Remote Repo         -     - Remote Repo         -
-                   -     - gets pushed         -     -                     -
- @ R01             -     - @ R02               -     - @ R02               -
---------------------     -----------------------     -----------------------

---------------------     -----------------------     -----------------------
- Local Repo        -     - Local Repo          -     - Local Repo          -
- pull              -     -                     -     - pull                -
- @ R01             -     - @ R01               -     - @ R02               -
---------------------     -----------------------     -----------------------

---------------------     -----------------------     -----------------------
- Local Sandbox     -     - Local Sandbox       -     - Local Sandbox       -
- Checkout          -     - new work done       -     - merged with R02     -
- @ R01             -     - @ R01+              -     - @R02+               -
---------------------     -----------------------     -----------------------

This helped me understand why a fetch is pretty important.这帮助我理解了为什么 fetch 非常重要。

We simply say:我们简单地说:

git pull == git fetch + git merge

If you run git pull , you do not need to merge the data to local.如果运行git pull ,则不需要将数据合并到本地。 If you run git fetch , it means you must run git merge for getting the latest code to your local machine.如果您运行git fetch ,则意味着您必须运行git merge才能将最新代码获取到本地计算机。 Otherwise, the local machine code would not be changed without merge.否则,不合并就不会更改本地机器代码。

So in the Git Gui, when you do fetch, you have to merge the data.所以在 Git Gui 中,当你获取数据时,你必须合并数据。 Fetch itself won't make the code changes at your local. Fetch 本身不会在您的本地进行代码更改。 You can check that when you update the code by fetching once fetch and see;您可以通过获取一次获取并查看来检查更新代码时; the code it won't change.它不会改变的代码。 Then you merge... You will see the changed code.然后合并...您将看到更改后的代码。

In simple terms, if you were about to hop onto a plane without any Internet connection… before departing you could just do git fetch origin <branch> .简单来说,如果您要在没有任何 Internet 连接的情况下跳上飞机……在出发之前,您可以执行git fetch origin <branch> It would fetch all the changes into your computer, but keep it separate from your local development/workspace.它会将所有更改提取到您的计算机中,但将其与您的本地开发/工作区分开。

On the plane, you could make changes to your local workspace and then merge it with what you've previously fetched and then resolve potential merge conflicts all without a connection to the Internet.在飞机上,您可以对本地工作区进行更改,然后将其与您之前获取的内容合并,然后解决潜在的合并冲突,所有这些都无需连接到 Internet。 And unless someone had made new changes to the remote repository then once you arrive at the destination you would do git push origin <branch> and go get your coffee.除非有人对远程存储库进行了的更改,否则一旦您到达目的地,您将执行git push origin <branch>并去喝咖啡。


From this awesome Atlassian tutorial :从这个很棒的 Atlassian 教程中

The git fetch command downloads commits, files, and refs from a remote repository into your local repository. git fetch命令将提交、文件和引用从远程存储库下载到本地存储库。

Fetching is what you do when you want to see what everybody else has been working on.当您想查看其他人一直在做什么时,您会执行获取操作。 It's similar to SVN update in that it lets you see how the central history has progressed, but it doesn't force you to actually merge the changes into your repository.它类似于 SVN 更新,因为它可以让您查看中央历史记录的进展情况,但它不会强制您将更改实际合并到您的存储库中。 Git isolates fetched content as a from existing local content , it has absolutely no effect on your local development work . Git将获取的内容与现有的本地内容隔离开来,它对您的本地开发工作绝对没有影响 Fetched content has to be explicitly checked out using the git checkout command.必须使用git checkout命令明确签出获取的内容。 This makes fetching a safe way to review commits before integrating them with your local repository.这使得在将提交与本地存储库集成之前,获取一种安全的方式来审查提交。

When downloading content from a remote repository, git pull and git fetch commands are available to accomplish the task.从远程存储库下载内容时,可以使用git pullgit fetch命令来完成任务。 You can consider git fetch the 'safe' version of the two commands.您可以考虑git fetch这两个命令的“安全”版本。 It will download the remote content, but not update your local repository's working state, leaving your current work intact.它将下载远程内容,但不会更新本地存储库的工作状态,从而使您当前的工作保持不变。 git pull is the more aggressive alternative, it will download the remote content for the active local branch and immediately execute git merge to create a merge commit for the new remote content. git pull是更激进的选择,它将下载活动本地分支的远程内容并立即执行git merge为新的远程内容创建合并提交。 If you have pending changes in progress this will cause conflicts and kickoff the merge conflict resolution flow.如果您有正在进行的未决更改,这将导致冲突并启动合并冲突解决流程。


With git pull :使用git pull

  • You don't get any isolation.你没有得到任何孤立。
  • It doesn't need to be explicitly checked out.它不需要显式检出。 Because it implicitly does a git merge .因为它隐含地做了一个git merge
  • The merging step will affect your local development and may cause conflicts合并步骤会影响你本地的发展,可能会造成冲突
  • It's basically NOT safe.这基本上是不安全的。 It's aggressive.这是侵略性的。
  • Unlike git fetch where it only affects your .git/refs/remotes , git pull will affect both your .git/refs/remotes and .git/refs/heads/不像git fetch它只影响你的.git/refs/remotes , git pull 会影响你的.git/refs/remotes.git/refs/heads/

Hmmm...so if I'm not updating the working copy with git fetch , then where am I making changes?嗯...所以如果我不使用git fetch更新工作副本,那么我在哪里进行更改? Where does Git fetch store the new commits? Git fetch 在哪里存储新的提交?

Great question.好问题。 First and foremost, the heads or remotes don't store the new commits.首先, headsremotes不存储新的提交。 They just have pointers to commits.他们只是有提交的指针 So with git fetch you download the latest git objects (blob, tree, commits. To fully understand the objects watch this video on git internals ), but only update your remotes pointer to point to the latest commit of that branch.因此,使用git fetch您可以下载最新的git 对象(blob、tree、commits。要完全了解这些对象,请在 git internals 上观看此视频),但只需更新您的remotes指针以指向该分支的最新提交。 It's still isolated from your working copy, because your branch's pointer in the heads directory hasn't updated.它仍然与您的工作副本隔离,因为您的分支在heads目录中的指针尚未更新。 It will only update upon a merge / pull .它只会在merge / pull时更新。 But again where?但又在哪里? Let's find out.让我们来了解一下。

In your project directory (ie, where you do your git commands) do:在您的项目目录中(即,您执行git命令的位置)执行以下操作:

  1. ls . ls This will show the files & directories.这将显示文件和目录。 Nothing cool, I know.没什么酷的,我知道。

  2. Now do ls -a .现在做ls -a This will show dot files , ie, files beginning with .这将显示点文件,即以 . 开头的文件. You will then be able to see a directory named: .git .然后,您将能够看到一个名为: .git的目录。

  3. Do cd .git .cd .git This will obviously change your directory.这显然会改变你的目录。

  4. Now comes the fun part;有趣的来了; do ls .ls You will see a list of directories.您将看到目录列表。 We're looking for refs .我们正在寻找refs Do cd refs .cd refs

  5. It's interesting to see what's inside all directories, but let's focus on two of them.查看所有目录中的内容很有趣,但让我们关注其中的两个。 heads and remotes . headsremotes Use cd to check inside them too.也可以使用cd检查它们的内部。

  6. Any git fetch that you do will update the pointer in the /.git/refs/remotes directory.您执行的任何git fetch操作都会更新/.git/refs/remotes目录中的指针。 It won't update anything in the /.git/refs/heads directory.它不会更新/.git/refs/heads目录中的任何内容。

  7. Any git pull will first do the git fetch , update items in the /.git/refs/remotes directory, then merge with your local and then change the head inside the /.git/refs/heads directory.任何git pull都会首先执行git fetch ,更新/.git/refs/remotes目录中的项目,然后与本地合并,然后更改/.git/refs/heads目录中的头。


A very good related answer can also be found in Where does 'git fetch' place itself?一个非常好的相关答案也可以在“git fetch”放在哪里? . .

Also, look for "Slash notation" from the Git branch naming conventions post.另外,从Git 分支命名约定帖子中查找“斜线表示法”。 It helps you better understand how Git places things in different directories.它可以帮助你更好地理解 Git 如何将东西放在不同的目录中。


To see the actual difference查看实际差异

Just do:做就是了:

git fetch origin master
git checkout master

If the remote master was updated you'll get a message like this:如果远程主机已更新,您将收到如下消息:

Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

If you didn't fetch and just did git checkout master then your local git wouldn't know that there are 2 commits added.如果您没有fetch并且只是做了git checkout master那么您的本地 git 不会知道添加了 2 个提交。 And it would just say:它只会说:

Already on 'master'
Your branch is up to date with 'origin/master'.

But that's outdated and incorrect.但这是过时和不正确的。 It's because git will give you feedback solely based on what it knows.这是因为 git 只会根据它所知道的信息给你反馈。 It's oblivious to new commits that it hasn't pulled down yet...它忘记了它尚未撤消的新提交......


Is there any way to see the new changes made in remote while working on the branch locally?在本地处理分支时,有什么方法可以查看远程所做的新更改?

Some IDEs (eg Xcode) are super smart and use the result of a git fetch and can annotate the lines of code that have been changed in remote branch of your current working branch.一些 IDE(例如 Xcode)非常智能,并且使用git fetch的结果,并且可以注释在当前工作分支的远程分支中已更改的代码行。 If that line has been changed by both local changes and remote branch, then that line gets annotated with red.如果该行已被本地更改和远程分支更改,则该行将被标记为红色。 This isn't a merge conflict.这不是合并冲突。 It's a potential merge conflict.这是一个潜在的合并冲突。 It's a headsup that you can use to resolve the future merge conflict before doing git pull from the remote branch.这是一个提示,您可以在从远程分支执行git pull之前使用它来解决未来的合并冲突。

在此处输入图像描述


Fun tip:有趣的提示:

If you fetched a remote branch eg did:如果您获取远程分支,例如:

git fetch origin feature/123

Then this would go into your remotes directory.然后这将进入您的远程目录。 It's still not available to your local directory.您的本地目录仍然无法使用它。 However, it simplifies your checkout to that remote branch by DWIM (Do what I mean):但是,它通过 DWIM 简化了您对该远程分支的结帐(按我的意思做):

git checkout feature/123

you no longer need to do:你不再需要做:

git checkout -b feature/123 origin/feature/123

For more on that read here有关更多信息,请在此处阅读

git fetch pulls down the code from the remote server to your tracking branches in your local repository. git fetch将代码从远程服务器下拉到本地存储库中的跟踪分支。 If your remote is named origin (the default) then these branches will be within origin/ , for example origin/master , origin/mybranch-123 , etc. These are not your current branches, they are local copies of those branches from the server.如果您的远程被命名为origin (默认),那么这些分支将位于origin/内,例如origin/masterorigin/mybranch-123等。这些不是您当前的分支,它们是来自服务器的这些分支的本地副本.

git pull does a git fetch but then also merges the code from the tracking branch into your current local version of that branch. git pull执行git fetch ,但随后还将跟踪分支中的代码合并到该分支的当前本地版本中。 If you're not ready for that changes yet, just git fetch first.如果您还没有准备好进行更改,请先git fetch

git fetch will retrieve remote branches so that you can git diff or git merge them with the current branch. git fetch将检索远程分支,以便您可以git diffgit merge它们与当前分支合并。 git pull will run fetch on the remote brach tracked by the current branch and then merge the result. git pull将在当前分支跟踪的远程分支上运行 fetch,然后合并结果。 You can use git fetch to see if there are any updates to the remote branch without necessary merging them with your local branch.您可以使用git fetch查看远程分支是否有任何更新,而无需将它们与本地分支合并。

Git Fetch Git 获取

You download changes to your local branch from origin through fetch.您通过 fetch 将更改从 origin 下载到本地分支。 Fetch asks the remote repo for all commits that others have made but you don't have on your local repo. Fetch 向远程存储库询问其他人已提交但您在本地存储库中没有的所有提交。 Fetch downloads these commits and adds them to the local repository. Fetch 下载这些提交并将它们添加到本地存储库。

Git Merge Git 合并

You can apply changes downloaded through fetch using the merge command.您可以使用 merge 命令应用通过 fetch 下载的更改。 Merge will take the commits retrieved from fetch and try to add them to your local branch. Merge 将获取从 fetch 中检索到的提交,并尝试将它们添加到您的本地分支。 The merge will keep the commit history of your local changes so that when you share your branch with push, Git will know how others can merge your changes.合并将保留您本地更改的提交历史,以便当您通过推送共享您的分支时,Git 将知道其他人如何合并您的更改。

Git Pull Git 拉取

Fetch and merge run together often enough that a command that combines the two, pull, was created. fetch 和 merge 经常一起运行,以至于创建了一个将两者结合在一起的命令 pull。 Pull does a fetch and then a merge to add the downloaded commits into your local branch.拉取然后合并以将下载的提交添加到您的本地分支。

The only difference between git pull and git fetch is that : git pullgit fetch之间的唯一区别是:

git pull pulls from a remote branch and merges it. git pull从远程分支中拉取并合并它。

git fetch only fetches from the remote branch but it does not merge git fetch仅从远程分支获取,但不合并

ie git pull = git fetch + git merge ...即 git pull = git fetch + git merge ...

Git allows chronologically older commits to be applied after newer commits. Git 允许在较新的提交之后应用按时间顺序较旧的提交。 Because of this, the act of transferring commits between repositories is split into two steps:因此,在存储库之间传输提交的行为分为两个步骤:

  1. Copying new commits from remote branch to copy of this remote branch inside local repo.将远程分支的新提交复制到本地仓库中此远程分支的副本。

    (repo to repo operation) master@remote >> remote/origin/master@local (repo 到 repo 操作) master@remote >> remote/origin/master@local

  2. Integrating new commits to local branch将新提交集成到本地分支

    (inside-repo operation) remote/origin/master@local >> master@local (内部仓库操作) remote/origin/master@local >> master@local

There are two ways of doing step 2. You can:第 2 步有两种方法。您可以:

  1. Fork local branch after last common ancestor and add new commits parallel to commits which are unique to local repository, finalized by merging commit, closing the fork.在最后一个共同祖先之后分叉本地分支,并添加与本地存储库唯一的提交并行的新提交,通过合并提交完成,关闭分叉。
  2. Insert new commits after last common ancestor and reapply commits unique to local repository.在最后一个共同祖先之后插入新提交并重新应用本地存储库独有的提交。

In git terminology, step 1 is git fetch , step 2 is git merge or git rebasegit术语中,第 1 步是git fetch ,第 2 步是git mergegit rebase

git pull is git fetch and git merge git pullgit fetchgit merge

The git pull command is actually a shortcut for git fetch followed by the git merge or the git rebase command depending on your configuration. git pull命令实际上是git fetch后跟git mergegit rebase命令的shortcut ,具体取决于您的配置。 You can configure your Git repository so that git pull is a fetch followed by a rebase.您可以配置您的 Git 存储库,以便git pull是 fetch 后跟 rebase。

Git obtains the branch of the latest version from the remote to the local using two commands: Git 使用两条命令从远程获取最新版本的分支到本地:

  1. git fetch: Git is going to get the latest version from remote to local, but it do not automatically merge. git fetch:Git 会从远程获取最新版本到本地,但不会自动合并。 git fetch origin master git log -p master..origin/master git merge origin/master git fetch origin master git log -p master..origin/master git merge origin/master

    The commands above mean that download latest version of the main branch from origin from the remote to origin master branch.上面的命令意味着将最新版本的主分支从远程从源下载到源主分支。 And then compares the local master branch and origin master branch.然后比较本地master分支和origin master分支。 Finally, merge.最后,合并。

  2. git pull: Git is going to get the latest version from the remote and merge into the local. git pull:Git 将从远程获取最新版本并合并到本地。

    git pull origin master

    The command above is the equivalent to git fetch and git merge .上面的命令相当于git fetchgit merge In practice, git fetch maybe more secure because before the merge we can see the changes and decide whether to merge.在实践中, git fetch可能更安全,因为在合并之前我们可以看到更改并决定是否合并。

What is the difference between git pull and git fetch ? git pullgit fetch有什么区别?

To understand this, you first need to understand that your local git maintains not only your local repository, but it also maintains a local copy of the remote repository.要理解这一点,您首先需要了解您的本地 git 不仅维护您的本地存储库,而且还维护远程存储库的本地副本。

git fetch brings your local copy of the remote repository up to date. git fetch使您的远程存储库的本地副本保持最新。 For example, if your remote repository is GitHub - you may want to fetch any changes made in the remote repository to your local copy of it the remote repository.例如,如果您的远程存储库是 GitHub - 您可能希望将远程存储库中所做的任何更改提取到远程存储库的本地副本。 This will allow you to perform operations such as compare or merge.这将允许您执行比较或合并等操作。

git pull on the other hand will bring down the changes in the remote repository to where you keep your own code.另一方面, git pull会将远程存储库中的更改带到您保存自己代码的位置。 Typically, git pull will do a git fetch first to bring the local copy of the remote repository up to date, and then it will merge the changes into your own code repository and possibly your working copy.通常, git pull将首先执行git fetch以使远程存储库的本地副本保持最新,然后它将更改合并到您自己的代码存储库和可能的工作副本中。

A simple Graphical Representation for Beginners,初学者的简单图形表示,

在此处输入图像描述

here,这里,

git pull  

will fetch code from repository and rebase with your local... in git pull there is possibility of new commits getting created.将从存储库中获取代码并使用您的本地重新设置...在 git pull 中,有可能创建新的提交。

but in ,但在 ,

git fetch获取

will fetch code from repository and we need to rebase it manually by using git rebase将从存储库中获取代码,我们需要使用git rebase手动对其进行变基

eg: i am going to fetch from server master and rebase it in my local master.例如:我将从服务器主服务器获取并在我的本地主服务器中重新设置它。

1) git pull ( rebase will done automatically): 1) git pull ( rebase 会自动完成):

git pull origin master

here origin is your remote repo master is your branch这里origin是你的远程仓库master是你的分支

2) git fetch (need to rebase manually): 2)git fetch(需要手动变基):

git fetch origin master

it will fetch server changes from origin.它将从原点获取服务器更改。 and it will be in your local until you rebase it on your own.它将在您的本地,直到您自己重新设置它。 we need to fix conflicts manually by checking codes.我们需要通过检查代码手动修复冲突。

git rebase origin/master

this will rebase code into local.这会将代码变基为本地代码。 before that ensure you're in right branch.在此之前确保您在正确的分支中。

git pull == ( git fetch + git merge) git pull == ( git fetch + git 合并)

git fetch does not changes to local branches. git fetch 不会更改本地分支。

If you already have a local repository with a remote set up for the desired project, you can grab all branches and tags for the existing remote using git fetch .如果您已经有一个为所需项目设置了远程的本地存储库,则可以使用 git fetch 获取现有远程的所有分支和标签。 ... Fetch does not make any changes to local branches, so you will need to merge a remote branch with a paired local branch to incorporate newly fetch changes. ... Fetch 不会对本地分支进行任何更改,因此您需要将远程分支与配对的本地分支合并以合并新的 fetch 更改。 from github来自 github

Actually Git maintains a copy of your own code and the remote repository.实际上,Git 维护您自己的代码和远程存储库的副本。

The command git fetch makes your local copy up to date by getting data from remote repository.命令git fetch通过从远程存储库获取数据使您的本地副本保持最新。 The reason we need this is because somebody else might have made some changes to the code and you want to keep yourself updated.我们需要这个的原因是因为其他人可能对代码进行了一些更改,而您希望自己保持更新。

The command git pull brings the changes in the remote repository to where you keep your own code.命令git pull将远程存储库中的更改带到您保存自己代码的位置。 Normally, git pull does this by doing a 'git fetch' first to bring the local copy of the remote repository up to date, and then it merges the changes into your own code repository and possibly your working copy.通常, git pull通过首先执行“git fetch”来使远程存储库的本地副本保持最新,然后将更改合并到您自己的代码存储库和可能的工作副本中。

git pull = git fetch + git merge 

git pull git 拉

It performs two functions using a single command.它使用一个命令执行两个功能。

It fetches all the changes that were made to the remote branch and then merges those changes into your local branch.它获取对远程分支所做的所有更改,然后将这些更改合并到您的本地分支中。 You can also modify the behaviour of pull by passing --rebase.您还可以通过传递 --rebase 来修改 pull 的行为。 The difference between merge and rebase can be read here可以在这里阅读合并和变基之间的区别

git fetch获取

Git fetch does only half the work of git pull. Git fetch 只完成了 git pull 的一半工作。 It just brings the remote changes into your local repo but does not apply them onto your branches.You have to explicitly apply those changes.它只是将远程更改带入您的本地存储库,但不会将它们应用到您的分支。您必须明确应用这些更改。 This can be done as follows:这可以按如下方式完成:

git fetch
git rebase origin/master

One must keep in mind the nature of git.必须牢记 git 的性质。 You have remotes and your local branches ( not necessarily the same ) .您有遥控器和本地分支机构(不一定相同)。 In comparison to other source control systems this can be a bit perplexing.与其他源代码控制系统相比,这可能有点令人困惑。

Usually when you checkout a remote a local copy is created that tracks the remote.通常,当您签出远程时,会创建一个跟踪远程的本地副本。

git fetch will work with the remote branch and update your information. git fetch 将与远程分支一起使用并更新您的信息。

It is actually the case if other SWEs are working one the same branch, and rarely the case in small one dev - one branch - one project scenarios.如果其他 SWE 正在同一个分支工作,实际上就是这种情况,而在小型开发 - 一个分支 - 一个项目场景中很少出现这种情况。

Your work on the local branch is still intact.您在当地分支机构的工作仍然完好无损。 In order to bring the changes to your local branch you have to merge/rebase the changes from the remote branch.为了将更改带到您的本地分支,您必须合并/重新设置远程分支的更改。

git pull does exactly these two steps ( ie --rebase to rebase instead of merge ) git pull 正是这两个步骤(即 --rebase 到 rebase 而不是 merge )

If your local history and the remote history have conflicts the you will be forced to do the merge during a git push to publish your changes.如果您的本地历史和远程历史有冲突,您将被迫在 git push 期间进行合并以发布您的更改。

Thus it really depends on the nature of your work environment and experience what to use.因此,这实际上取决于您的工作环境的性质和使用什么的经验。

All branches are stored in .git/refs所有分支都存储在.git/refs

All local branches are stored in .git/refs/heads所有本地分支都存储在.git/refs/heads

All remote branches are stored in .git/refs/remotes所有远程分支都存储在.git/refs/remotes

The git fetch command downloads commits, files, and refs from a remote repository into your local repo. git fetch命令将提交、文件和引用从远程存储库下载到本地存储库。 Fetching is what you do when you want to see what everybody else has been working on.当您想查看其他人一直在做什么时,您会执行获取操作。

So when you do git fetch all the files, commits, and refs are downloaded in因此,当您执行git fetch时,所有文件、提交和引用都会下载到

this directory .git/refs/remotes这个目录.git/refs/remotes

You can switch to these branches to see the changes.您可以切换到这些分支以查看更改。

Also, you can merge them if you want.此外,您可以根据需要合并它们。

git pull just downloads these changes and also merges them to the current branch. git pull只是下载这些更改并将它们合并到当前分支。

Example例子

If you want see the work of remote branch dev/jd/feature/auth , you just need to do如果你想看到远程分支dev/jd/feature/auth的工作,你只需要做

git fetch origin dev/jd/feature/auth

to see the changes or work progress do,查看更改或工作进度,

git checkout dev/jd/feature/auth

But, If you also want to fetch and merge them in the current branch do,但是,如果您还想在当前分支中获取并合并它们,

git pull origin dev/jd/feature/auth

If you do git fetch origin branch_name , it will fetch the branch, now you can switch to this branch you want and see the changes.如果您执行git fetch origin branch_name ,它将获取分支,现在您可以切换到您想要的分支并查看更改。 Your local master or other local branches won't be affected.您的本地 master 或其他本地分支不会受到影响。 But git pull origin branch_name will fetch the branch and will also merge to the current branch.但是git pull origin branch_name将获取分支并合并到当前分支。

Simple explanation:简单解释:

git fetch

fetches the metadata.获取元数据。 If you want to check out a recently created branch you may want to do a fetch before checkout.如果您想签出最近创建的分支,您可能需要在结帐前进行提取。

git pull

Fetches the metadata from remote and also moved the files from remote and merge on to the branch从远程获取元数据,并将文件从远程移动并合并到分支

Git Fetch Git 获取

Helps you to get known about the latest updates from a git repository .帮助您了解来自git repository的最新更新。 Let's say you working in a team using GitFlow , where team working on multiple branches ( features ).假设您在使用GitFlow的团队中工作,团队在多个branches (功能)上工作。 With git fetch --all command you can get known about all new branches within repository .使用git fetch --all command ,您可以了解repository中的所有新branches

Mostly git fetch is used with git reset .大多数情况下git fetchgit reset一起使用。 For example you want to revert all your local changes to the current repository state.例如,您希望将所有本地更改恢复到当前存储库状态。

git fetch --all // get known about latest updates
git reset --hard origin/[branch] // revert to current branch state

Git pull git拉

This command update your branch with current repository branch state.此命令使用当前repository branch状态更新您的branch Let's continue with GitFlow .让我们继续GitFlow Multiple feature branches was merged to develop branch and when you want to develop new features for the project you must go to the develop branch and do a git pull to get the current state of develop branch多个功能branches mergeddevelop分支,当您想为项目开发新功能时,您必须转到开发branch并执行git pull以获取develop branch的当前状态

Documentation for GitFlow https://gist.github.com/peterdeweese/4251497 GitFlow 文档https://gist.github.com/peterdeweese/4251497

This graphic could be of help.这张图可能会有所帮助。 git pull is essentially equivalent to git fetch then git merge git pull本质上等同于git fetch然后git merge

这张图可能会有所帮助。 git pull 本质上等同于 git fetch 然后 git merge

By Definition:按定义:

Git Fetch: Git获取:

Git fetch is a command that allows you to download objects from another repository. Git fetch 是一个允许您从另一个存储库下载对象的命令。

Git Pull: Git 拉取:

Git pull is a command that allows you to fetch from and integrate with another repository or local branch. Git pull 是一个命令,它允许您从另一个存储库或本地分支获取并与之集成。

Key Differences主要区别

When should you use Git Fetch?什么时候应该使用 Git Fetch?

If you only want to see all of the current branches and changes in your remote repository, Git fetch can get you all of the information you need without actually making any local changes to your work.如果您只想查看远程存储库中的所有当前分支和更改,Git fetch 可以获取您需要的所有信息,而无需实际对您的工作进行任何本地更改。

  • This gives you time to decide on the best course of action for incorporating your changes, such as merging them in or fast-forwarding your local branch.这使您有时间决定合并更改的最佳行动方案,例如将它们合并到本地分支中或快速转发。

Command:命令:

$ git fetch origin $ git 获取原点

When should you use Git Pull?什么时候应该使用 Git Pull?

Git pull is a desirable action when you have complete context about the changes you will be getting from your remote repository and adding to your local copy.当您对将从远程存储库获取并添加到本地副本的更改有完整的上下文时,Git pull 是一个理想的操作。

Command:命令:

$ git pull origin master $ git pull 起源大师

Fact:事实:

Both are used to download new data from a remote repository.两者都用于从远程存储库下载新数据。

Why You need these commands?为什么你需要这些命令?

for example, there were changes recently made to your remote repository and you want to incorporate them into your local copy.例如,最近对您的远程存储库进行了更改,您希望将它们合并到您的本地副本中。 You have a few options here;你有几个选择; the two most common actions to get changes from your remote are Git pull and Git fetch.从远程获取更改的两个最常见的操作是 Git pull 和 Git fetch。

Points To Consider:需要考虑的要点:

Fetch:拿来:

  • Its main function is to fetch the content.它的主要功能是获取内容。
  • It has only command-line syntax.它只有命令行语法。

Pull:拉:

  • Its main function is a combination of fetch and merges the content.它的主要功能是获取和合并内容的组合。
  • It has command-line syntax as well as a pull request to post the changes.它具有命令行语法以及发布更改的拉取请求。

My Opinion:我的意见:

The Git Pull is better than git fetch as it does the functionality of two commands. Git Pull 比 git fetch 更好,因为它具有两个命令的功能。 However, Git Fetch is considered a safer one compared to Git Pull.但是,与 Git Pull 相比,Git Fetch 被认为更安全。

Fetch拿来

$ git fetch origin

git fetch only downloads new data from a remote repository - but it doesn't integrate any of this new data into your working files. git fetch 仅从远程存储库下载新数据 - 但它不会将任何这些新数据集成到您的工作文件中。 Fetch is great for getting a fresh view of all the things that happened in a remote repository. Fetch 非常适合对远程存储库中发生的所有事情进行全新的了解。 Due to its "harmless" nature, you can rest assured: fetch will never manipulate, destroy, or screw up anything.由于其“无害”的性质,您可以放心:fetch 永远不会操纵、破坏或搞砸任何东西。 This means you can never fetch often enough.这意味着您永远无法获取足够的频率。

Pull

$ git pull origin master

git pull, in contrast, is used with a different goal in mind: to update your current HEAD branch with the latest changes from the remote server.相比之下,使用 git pull 的目的不同:使用远程服务器的最新更改更新当前的 HEAD 分支。 This means that pull not only downloads new data;这意味着拉取不仅下载新数据; it also directly integrates it into your current working copy files.它还直接将其集成到您当前的工作副本文件中。

Fact:事实:

Both are used to download new data from a remote repository.两者都用于从远程存储库下载新数据。

Why do you need these commands?为什么需要这些命令?

For example, your remote repository has recently changed and you need to include them in your local copy.例如,您的远程存储库最近发生了更改,您需要将它们包含在本地副本中。 You have several options here;您在这里有几个选择; The two most common options for making changes to your remote control are Git pull and Git fetch.对遥控器进行更改的两个最常见的选项是 Git pull 和 Git fetch。

Points to consider:需要考虑的要点:

Fetch:拿来:

  • Its main function is to fetch the content.它的主要功能是获取内容。
  • There is only command-line syntax.只有命令行语法。

pull:拉:

  • Its main function is a combination of fetching and merging the content.它的主要功能是获取和合并内容的组合。
  • It has a command-line phrase as well as a pull request to post changes.它有一个命令行短语以及一个发布更改的拉取请求。

My opinion:我的意见:

Git Pull is better than git fetch because it executes two commands. Git Pull 比 git fetch 更好,因为它执行两个命令。 However, the Git Fetch is considered a safer one compared to the Git Pull.但是,与 Git Pull 相比,Git Fetch 被认为更安全。

A git repository contains immutable data blobs and a few mutable pointers/references/names (we call them branches, HEADS) for usability (in theory, git could be a pure append-only store, accessed only by commit hashes).一个 git 存储库包含不可变的数据 blob 和一些可变的指针/引用/名称(我们称它们为分支,HEADS)以提高可用性(理论上,git 可以是一个纯粹的 append-only 存储,只能通过提交哈希访问)。

The immutable blobs are usually always eventually shared among all contributors.不可变的 blob 通常最终总是在所有贡献者之间共享。 Every developer has a copy of all of these on his machine.每个开发人员在他的机器上都有所有这些的副本。

git fetch downloads the latest blobs and remote mutable files to your machine. git fetch将最新的 blob 和远程可变文件下载到您的机器上。

It does not change your mutable files nor create any blobs that did not previously exist somewhere.它不会更改您的可变文件,也不会创建任何以前不存在的 blob。

git pull is git fetch then git merge . git pullgit fetch然后是git merge git merge creates new blobs that never existed in the remote repository before and updates your mutable files (your references). git merge创建以前从未存在于远程存储库中的新 blob,并更新您的可变文件(您的引用)。

git fetch will tell the local git to retrieve the latest meta-data info from the original (No file transferring) . git fetch将告诉本地 git 从原始文件中retrieve the latest meta-data信息(No file transferring)

git pull = git fetch AND brings (copy) all changes from the remote repository . git pull = git 提取从远程存储库中获取(复制)所有更改 (Includes file transferring)

git pull also tries to merge with local changes. git pull也尝试与本地更改合并。

Fetch拿来

git fetch really only downloads new data from a remote repository - but it doesn't integrate any of this new data into your working files. git fetch 实际上只从远程存储库下载新数据 - 但它不会将任何这些新数据集成到您的工作文件中。 Fetch is great for getting a fresh view on all the things that happened in a remote repository. Fetch 非常适合获取远程存储库中发生的所有事情的全新视图。 Due to it's "harmless" nature, you can rest assured: fetch will never manipulate, destroy, or screw up anything.由于它的“无害”特性,您可以 rest 放心:fetch 永远不会操纵、破坏或搞砸任何东西。

Pull

git pull, in contrast, is used with a different goal in mind: to update your current HEAD branch with the latest changes from the remote server.相比之下,git pull 用于不同的目标:使用来自远程服务器的最新更改更新当前的 HEAD 分支。 This means that pull not only downloads new data;这意味着 pull 不仅下载新数据; it also directly integrates it into your current working copy files.它还直接将其集成到您当前的工作副本文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM