Moderator Note: Given that this question has already had sixty-seven answers posted to it (some of them deleted), consider whether or not you are contributing anything new before posting another one.
What are the differences between git pull
and git fetch
?
A simple Graphical Representation for Beginners,
here,
will fetch code from repository and rebase with your local... in git pull there is possibility of new commits getting created.
but in ,
git fetch
will fetch code from repository and we need to rebase it manually by using
git rebase
eg: i am going to fetch from server master and rebase it in my local master.
1) git pull ( rebase will done automatically):
here origin is your remote repo master is your branch
2) git fetch (need to rebase manually):
it will fetch server changes from origin. and it will be in your local until you rebase it on your own. we need to fix conflicts manually by checking codes.
this will rebase code into local. before that ensure you're in right branch.
Briefly
git fetch
is similar topull
but doesn't merge. i.e. it fetches remote updates (refs
andobjects
) but your local stays the same (i.e.origin/master
gets updated butmaster
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
andgit 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):
After downloading the updates, let's see the differences:
If you're happy with those updates, then merge:
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: http://longair.net/blog/2009/04/16/git-fetch-and-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.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:
Sometimes a visual representation helps.
Bonus:
In speaking of pull & fetch in the above answers, I would like to share an interesting trick,
git pull --rebase
This above command is the most useful command in my git life which saved a lots of time.
Before pushing your new commits to server, try this command and it will automatically sync latest server changes (with a fetch + merge) and will place your commit at the top in git log. No need to worry about manual pull/merge.
Find details at: http://gitolite.com/git-pull--rebase