What are the differences between git pull
and git fetch
?
In the simplest terms, You can do a A Git documentation: git pull |
|||||||||||||||||||||
|
|
|||||||||||||||||||||
|
It is important to contrast the design philosophy of git with the philosophy of a more traditional source control tool like 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. 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. 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.
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. 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
|
|||||||||||||||||||||
|
It cost me a little bit to understand what was the difference, but this is a simple explanation. 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 when you start working and do commits you advance the master pointer to So the difference will be:
|
|||||||||||||||||
|
Here is Oliver Steele's image of how all it all fits together: If there is sufficient interest, I suppose I could update the image to add |
|||||||||||||||||||||
|
Briefly
More
Also, This blog post was useful: The difference between git pull, git fetch and git clone (and git rebase) - Mike Pearce and covers ==== UPDATE I thought I'd update this to show how you'd actually use this in practice.
Notes: On step 2: For more on diffs between local and remotes, see: compare local git branch with remote branch? On step 3: It's probably more accurate (e.g. on a fast changing repo) to do a See also: http://longair.net/blog/2009/04/16/git-fetch-and-merge/ |
|||||||||||||
|
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. |
|||||||||||||
|
You can fetch from a remote repository, see the differences and then pull or merge. This is an example for a remote repository called
|
|||||
|
The short and easy answer is that It is very important to note that In addition to pulling and pushing, some workflows involve
If you find yourself in such a situation, you may be tempted to
|
|||||||||||||||||||||
|
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.
Some major advantages for having a fetched mirror of the remote are:
|
|||||||||||||||||||||
|
|
|||||
|
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 preformed over time while watching the version of them. So here is what I came up with. Please correct me if I messed up anywhere. The three repos with a fetch
The three repos with a pull
This helped me understand why a fetch is pretty important. |
|||||||||||||
|
We simply say:
If you run So in the Git Gui, when you do fetch, you have to merge the data. Fetch itself won't make the code changes at your local. 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. |
|||||||||
|
|
||||
|
|
||||
|
The only difference between
i.e. git pull = git fetch + git merge ... |
|||||
|
This graphical representation might be very helpful: http://ndpsoftware.com/git-cheatsheet.html
|
||||
|
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:
There are two ways of doing step 2. You can:
In
|
|||
|
Git obtains the branch of the latest version from the remote to the local using two commands:
|
|||
|
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 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 |
|||||
|
Bonus:In speaking of pull & fetch in the above answers, I would like to share an interesting trick,
|
Trying to be clear and simple. The git pull command is actually a |
|||
|
From Pro Git § 2.5 Git Basics - Working with Remotes: Fetching and Pulling from Your Remotes:
|
||||
|
|
|||
|
git pull 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. The difference between merge and rebase can be read here git fetch Git fetch does only half the work of 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 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.
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.
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. |
|||
|
From git cheat sheet:
|
|||
|
fetch vs pull fetch will download any changes from the remote branch, updating your repository data, but leaving your local branch unchanged. pull will perform a fetch and additionally merge the changes into your local branch. What's the difference? pull updates you local branch with changes from the pulled branch. A fetch does not advance your local branch. |
|||
|
protected by Brad Larson♦ Mar 10 '13 at 1:30
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
git fetch; git reset --hard origin/master
as part of our workflow. It blows away local changes, keeps you up to date with master BUT makes sure you don't just pull in new changes on top on current changes and make a mess. We've used it for a while and it basically feels a lot safer in practice. Just be sure to add/commit/stash any work-in-progress first ! – Michael Durrant May 4 '14 at 14:32