What are the differences between git pull
and git fetch
?
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 pull
is shorthand forgit fetch
followed bygit merge FETCH_HEAD
.
"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: 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 registeredgit remote
(meaning you pass a URL on thegit pull
command line).@espertus: No. Pushing never automatically does a merge. The user is expected to pull, resolving any merge conflicts locally, then push back to the remote.
If I am at
/home/alice/
and dogit fetch /home/bob
, what parameters should I pass to the subsequentgit merge
?Note to people learning Git:
pull
can't actually be emulated by afetch
plus amerge
. I just fetched a change where only a remote branch pointer changes, andmerge
refuses to do anything.pull
, on the other hand, fast-forwards my tracking branch.