tags:

views:

27150

answers:

4

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

+109  A: 

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 local copy of a remote branch. This operation never changes any of your own branches 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 your repository up to date with a remote repository.

Greg Hewgill
"A "git pull" is what you would do to bring your repository up to date" <- isn't the repository update already done by fetch? don't you mean it brings your local branches up-to-date with the remote branches?To the merge: It merges the remote branches with your local copies of those branches, or what exactly does it merge here?
Albert
@Albert: Yeah, it's weirdly worded. `git pull` will always merge into the **current branch**. So you select which branch you want to pull *from*, and it pulls it into the current branch. The *from* branch can be local or remote; it can even be a remote branch that's not a registered `git remote` (meaning you pass a URL on the `git pull` command line).
intuited
+5  A: 
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.

Vinko Vrsalovic
Very interesting, but I can't really see a use case where you want "just the code". Et what happen with your code when you fetch? Is it erased? What happen whith the remote changes? How does it goes into your repo whithout erasing your code if you don't merge?
e-satis
+6  A: 

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 working copy.

git fetch
git diff ...origin
mepster
Great addition! I was confused by the dots, isn't it: git diff origin
harm
+2  A: 

I found this well written article about git fetch and git pull it's worth the reading: http://longair.net/blog/2009/04/16/git-fetch-and-merge/

Marcos Oliveira