'git pull'和'git fetch'有什么区别?
git pull
和git fetch
什么区别?
最简单的说, git pull
执行一个git fetch
然后是一个git merge
。
你可以随时做一个git fetch
来更新远程追踪分支在refs/remotes/<remote>/
。
这个操作永远不会改变任何你自己的本地分支在refs/heads
,而且不需要改变你的工作副本是安全的。 我甚至听说有人在后台定期执行git fetch
工作(尽pipe我不build议这样做)。
一个git pull
是你会做一个本地分支最新的远程版本,同时也更新你的其他远程跟踪分支。
Git文档: git pull
-
当你使用
pull
,Git试图自动为你做你的工作。 它是上下文敏感的 ,所以Git会将任何拉的提交合并到你当前正在工作的分支中。pull会自动合并提交而不让你先查看它们 。 如果你不仔细pipe理你的分支机构,你可能会遇到频繁的冲突。 -
当你
fetch
,Git收集目标分支中不存在于当前分支中的任何提交,并将它们存储在本地存储库中 。 但是, 它不会将它们与您当前的分支合并 。 如果你需要保持你的版本库是最新版本的,那么这是特别有用的,但是如果你更新了你的文件,那么这个版本可能会被破坏。 要将提交集成到主分支中,请使用merge
。
将git的devise哲学与更传统的源代码控制工具(如svn)的哲学进行对比是非常重要的。
Subversion是用一个客户机/服务器模型devise和构build的。 有一个存储库是服务器,有几个客户端可以从服务器获取代码,对其进行处理,然后将其提交回服务器。 假设客户端在需要执行操作时可以随时联系服务器。
Git被devise为支持更多的分布式模型,而不需要中央仓库(尽pipe如果你愿意的话,你可以使用一个。)另外git的devise使得客户端和“服务器”不需要在线同时。 Git的devise使得不可靠链接上的用户甚至可以通过电子邮件交换代码。 可以完全断开连接并刻录CD以通过git交换代码。
为了支持这个模型,git使用你的代码维护一个本地仓库,还有一个额外的镜像远程仓库状态的本地仓库。 通过本地保存远程仓库的副本,即使远程仓库无法访问,git也可以找出所需的更改。 稍后,当你需要将更改发送给其他人时,git可以将它们作为远程存储库已知时间点的一组更改进行传输。
-
git fetch是这样的命令:“使我的远程仓库的本地副本保持最新状态”。
-
git pull说:“把我自己的代码保存在远程仓库中的变化。”
通常情况下,“ git pull ”通过执行“ git fetch ”来使远程仓库的本地副本更新,然后将更改合并到您自己的代码存储库以及可能的工作副本中。
拿走是要记住,您的工作站上至less有三个项目的副本 。 一个副本是您自己的存储库和您自己的提交历史logging。 第二个副本是您正在编辑和构build的工作副本。 第三个副本是远程存储库的本地“caching”副本。
奥利弗·斯蒂尔 ( Oliver Steele)的形象是如何融合在一起的 :
如果有足够的兴趣,我想我可以更新图像添加git clone
和git merge
…
一个使用git fetch
例子是,以下将告诉你自上次拉动以来远程分支的任何变化…所以你可以在做实际的拉动之前检查,这可能会改变当前分支和工作拷贝中的文件。
git fetch git diff ...origin
了解有什么不同,花了我一点时间,但这是一个简单的解释。 master
在你的本地主机是一个分支。
克隆存储库时,您可以将整个存储库读取到本地主机。 这意味着当时你有一个起始/主指针指向HEAD
,主指针指向同一个HEAD
。
当你开始工作,并提交你提前主指针HEAD
+你的提交。 但是起始/主指针仍然指向你克隆时的情况。
所以区别在于:
- 如果你做了一个
git fetch
它只会获取远程仓库( GitHub )中的所有更改,并将原始/主指针移到HEAD
。 与此同时,您当地的分行主将会一直指向它所在的位置。 - 如果你做一个
git pull
,它将基本上取(如前所述),并将任何新的更改合并到主分支,并将指针移到HEAD
。
简要地
git fetch
与pull
类似但不合并。 即它获取远程更新( refs
和objects
),但你的本地保持不变(即origin/master
更新,但master
保持不变)。
git pull
从远程拉下来并立即合并。
更多
git clone
克隆一个回购。
git rebase
将当前分支中不在上游分支中的东西保存到临时区域。 您的分支现在与您开始更改之前一样。 所以, git pull -rebase
会拉下远程更改,倒回你的本地分支,一个接一个地在当前分支的顶部重播你的改变,直到你是最新的。
另外, git branch -a
会告诉你到底是怎么回事你所有的分支 – 本地和远程。
这篇博文很有用:
git pull,git fetch和git clone(和git rebase)之间的区别 – Mike Pearce
并涵盖了git pull
, git fetch
, git clone
和git rebase
。
====
UPDATE
我想我会更新这个来展示你在实际中如何使用这个。
-
从远程更新您的本地回购(但不合并):
git fetch
-
下载更新后,我们来看看不同之处:
git diff主起源/主
-
如果您对这些更新感到满意,则合并:
混帐拉
笔记:
在步骤2:有关本地和远程之间差异的更多信息,请参阅: 比较本地git分支与远程分支?
在步骤3:这可能是更准确的(例如在一个快速变化的回购)在这里做一个git rebase origin
。 请参阅@Justin Ohms在另一个答案中的评论。
另见: http : //longair.net/blog/2009/04/16/git-fetch-and-merge/
有时一个视觉表示帮助。
git-pull - 从另一个存储库或本地分支获取并合并 概要 混帐拉... 描述 使用给定的参数运行git-fetch,并调用git-merge来合并 将头部检索到当前分支。 用--rebase,调用git-rebase 而不是git-merge。 请注意,您可以使用。 (当前目录)作为<repository>来拉取 从本地存储库 - 这在合并本地分支时很有用 进入当前分支。 还要注意,这个选项意味着git-pull本身和底层的git-merge 必须在git-fetch的选项之前给出。
如果您想要合并历史logging,您可以拉取,如果您只是想要“codez”,就像某个人在这里标记了一些文章一样。
您可以从远程存储库获取数据,查看差异,然后进行提取或合并。
这是一个名为origin
的远程仓库的例子,一个叫master
的分支跟踪远程分支的origin/master
:
git checkout master git fetch git diff origin/master git rebase origin master
简短的回答是, git pull
就是git fetch
然后是git merge
。
需要注意的是, 无论你喜不喜欢 , git pull
都会自动合并 。 这当然会导致合并冲突。 假设你的遥控器是origin
,你的分支是master
。 如果你在提取之前使用git diff origin/master
,你应该对潜在的合并冲突有所了解,并且可以相应地准备你的本地分支。
除了拉和推, 一些工作stream涉及git rebase
,比如这个,我从链接的文章中解释:
git pull origin master git checkout foo-branch git rebase master git push origin foo-branch
如果你发现自己处于这样的状况,那么你可能会被诱惑git pull --rebase
。 除非你真的,真的知道你在做什么,我会build议反对。 这个警告来自git-pull
版本2.3.5
的man
页:
这是一种潜在的危险操作模式。 它重写历史,当你发布历史时,这并不是一个好兆头。 除非你仔细阅读了git-rebase(1),否则不要使用这个选项。
我喜欢有一些视觉performance的情况来掌握这些东西。 也许其他开发人员也想看到它,所以这里是我的补充。 我不完全确定这一切是否正确,所以如果您发现任何错误,请发表评论。
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.
获取远程镜像的一些主要优点是:
- 性能 (滚动浏览所有提交和消息,而不尝试通过networking挤压)
- 关于您当地回购的状态的反馈 (例如,我使用Atlassian的SourceTree,它会给我一个灯泡,指出我是否比原始地位提前或落后,这个信息可以用GIT FETCH来更新)。
奖金:
在上面的回答中,我想分享一个有趣的窍门,
git pull --rebase
这个命令是我的git生命中最有用的命令,它节省了大量的时间。
在将新的提交推送到服务器之前,请尝试使用此命令,它会自动同步最新的服务器更改(使用提取+合并),并将提交放在git日志的顶部。 无需担心手动拉/合并。
这种交互式的graphics表示在下面的git中很有帮助: http : //ndpsoftware.com/git-cheatsheet.html
git fetch
只是将远程的更改“下载”到本地存储库。 git pull
下载更改并将它们合并到您当前的分支中。 “在默认模式下, git pull
是git fetch
缩写,后面是git merge FETCH_HEAD
。”
我也一直在努力。 事实上,我在这里与谷歌search完全相同的问题。 阅读所有这些答案后,终于在我脑海中画了一张照片,我决定试着弄清楚这两个存储库和一个沙箱的状态,并观察它们的版本。 所以这是我想出来的。 请纠正我,如果我在任何地方搞砸了。
三个回购取回:
--------------------- ----------------------- ----------------------- - 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+ - --------------------- ----------------------- -----------------------
三拉回与回购
--------------------- ----------------------- ----------------------- - 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+ - --------------------- ----------------------- -----------------------
这帮助我理解了为什么取回是非常重要的。
我们只是说:
git pull == git fetch + git merge
如果你运行git pull
,你不需要将数据合并到本地。 如果你运行git fetch
,这意味着你必须运行git merge
来获取最新的代码到本地机器。 否则,本地机器代码将不会被合并更改。
所以在Git Gui中,当你获取数据时,你必须合并数据。 取本身不会使你的本地代码发生变化。 你可以检查一下,当你更新代码时,通过抓取一次就可以看到; 它的代码不会改变。 然后你合并…你会看到更改的代码。
git fetch
pulls down the code from the remote server to your tracking branches in your local repository. 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.
git pull
does a git fetch
but then also merges the code from the tracking branch into your current local version of that branch. If you're not ready for that changes yet, just git fetch
first.
git fetch
will retrieve remote branches so that you can git diff
or git merge
them with the current branch. git pull
will run fetch on the remote brach tracked by the current branch and then merge the result. 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
You download changes to your local branch from origin through fetch. Fetch asks the remote repo for all commits that others have made but you don't have on your local repo. Fetch downloads these commits and adds them to the local repository.
Git Merge
You can apply changes downloaded through fetch using the merge command. Merge will take the commits retrieved from fetch and try to add them to your local branch. 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 Pull
Fetch and merge run together often enough that a command that combines the two, pull, was created. 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 pull
pulls from a remote branch and merges it.
git fetch
only fetches from the remote branch but it does not merge
ie git pull = git fetch + git merge …
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)
Let's take a example that You are working on a project with your team members. So their 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- (AB,C are Modules already completed of the project)
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'. 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 main branch can lead to conflict and may cause your Module 'D' to malfunction.
To avoid such issues and to work parallel with the original progress of the project their 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. And will wait for the Git Merge command to apply the changes that have been fetched to your Repository or branch.
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'.
2. Git Pull- This will update your local branch with the origin/main branch ie actually what it does is combination of Git Fetch and Git merge one after another. But this may Cause Conflicts to occur, so it's recommended to use Git Pull with a clean copy.
Git allows chronologically older commits to be applied after newer commits. Because of this, the act of transferring commits between repositories is split into two steps:
-
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
-
Integrating new commits to local branch
(inside-repo operation)
remote/origin/master@local >> master@local
There are two ways of doing step 2. You can:
- 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.
- 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 rebase
git pull
is git fetch
and git merge
What is the difference between
git pull
andgit 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 fetch
brings your local copy of the remote repository up to date. 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. 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. 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 obtains the branch of the latest version from the remote to the local using two commands:
-
git fetch: Git is going to get the latest version from remote to local, but it do not automatically merge.
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. Finally, merge.
-
git pull: Git is going to get the latest version from the remote and merge into the local.
git pull origin master
The command above is the equivalent to
git fetch
andgit merge
. In practice,git fetch
maybe more secure because before the merge we can see the changes and decide whether to merge.
OK , Here are some information about git pull
and git fetch
, so you can understand the actual differences… in simple words, fetch gets the latest data, but not the code changes and not going to mess with your current code, but pull get the code changes and merge it your local branch, read on to get more details about each:
git fetch
It will download all refs and object and any new branches to 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. By using a refspec that fetches tags explicitly, you can fetch tags that do not point into branches you are interested in as well.
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. entry in the configuration file. (See 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. This information may be used by scripts or other git commands, such as git-pull.
混帐拉
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.
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. With –rebase, it runs git rebase instead of git merge.
should be the name of a remote repository as passed to 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.
Default values for and are read from the "remote" and "merge" configuration for the current branch as set by git-branch –track.
I also create the visual below to show you how git fetch
and git pull
working together…
git pull == ( git fetch + git merge)
git fetch does not changes to local branches.
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 . … 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. from github
Trying to be clear and simple.
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. You can configure your Git repository so that git pull is a fetch followed by a rebase.
Actually Git maintains a copy of your own code and the remote repository.
The command git fetch
makes your local copy up to date by getting data from remote repository. 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. 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.
From Pro Git § 2.5 Git Basics – Working with Remotes: Fetching and Pulling from Your Remotes :
It's important to note that the
fetch
command pulls the data to your local repository — it doesn't automatically merge it with any of your work or modify what you're currently working on. You have to merge it manually into your work when you're ready.If you have a branch set up to track a remote branch, you can use the
git pull
command to automatically fetch and then merge a remote branch into your current branch. This may be an easier or more comfortable workflow for you; and by default, thegit clone
command automatically sets up your local master branch to track the remote master branch on the server you cloned from (assuming the remote has a master branch). Runninggit pull
generally fetches data from the server you originally cloned from and automatically tries to merge it into the code you're currently working on.
Git Pull:
From what I understand, git pull will pull down from a remote whatever you ask (so, whatever trunk you're asking for) and instantly merge it into the branch you're in when you make the request. Pull is a high-level request that runs 'fetch' then a 'merge' by default, or a rebase with '–rebase'. You could do without it, it's just a convenience.
%> git checkout localBranch %> git pull origin master %> git branch master * localBranch
The above will merge the remote “master” branch into the local “localBranch”.
Git fetch:
Fetch is similar to pull, except it won't do any merging.
%> git checkout localBranch %> git fetch origin remoteBranch %> git branch master * localBranch remoteBranch
So, the fetch will have pulled down the remoteBranch and put it into a local branch called “remoteBranch”. creates a local copy of a remote branch which you shouldn't manipulate directly; instead create a proper local branch and work on that. 'git checkout' has a confusing feature though. If you 'checkout' a local copy of a remote branch, it creates a local copy and sets up a merge to it by default.