What are the differences between git pull and git fetch?
39 Answers
In the simplest terms, git pull does a git fetch followed by a git merge.
You can do a git fetch at any time to update your remote-tracking branches under 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 fetch periodically in a cron job in the background (although I wouldn't recommend doing this).
A git pull is what you would do to bring a local branch up-to-date with its remote version, while also updating your other remote-tracking branches.
From the Git documentation for git pull:
In its default mode,
git pullis shorthand forgit fetchfollowed bygit merge FETCH_HEAD.
When you use
pull, Git tries to automatically merge. It is context sensitive, so Git will merge any pulled commits into the branch you are currently working on.pullautomatically merges the commits without letting you review them first. If you don’t carefully 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 current branch, you must usemergeafterwards.
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.
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."
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.
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 git clone and git merge...
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 diff ...origin
See: regarding double- and triple-dot syntax in the diff command
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.
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.
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.
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 toHEAD. 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 toHEAD.
Sometimes a visual representation helps.
Briefly
git fetch is similar to pull but doesn't merge. i.e. it fetches remote updates (refs and objects) but your local stays the same (i.e. origin/master gets updated but master stays the same) .
git pull pulls down from a remote and instantly merges.
More
git clone clones a repo.
git rebase saves 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 -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.
Also, git branch -a will show you exactly what’s going on with all your branches - local and remote.
This blog post was useful:
The difference between git pull, git fetch and git clone (and git rebase) - Mike Pearce
and covers git pull, git fetch, git clone and git rebase.
UPDATE
I thought I'd update this to show how you'd actually use this in practice.
Update your local repo from the remote (but don't merge):
git fetchAfter downloading the updates, let's see the differences:
git diff master origin/masterIf 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?
On step 3: It's probably more accurate (e.g. on a fast changing repo) to do a git rebase origin here. See @Justin Ohms comment in another answer.
See also:
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.
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: