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
?
You can fetch from a remote repository, see the differences and then pull or merge.
This is an example for a remote repository called
origin
and a branch calledmaster
tracking the remote branchorigin/master
:The only difference between
git pull
andgit fetch
is that :git pull
pulls from a remote branch and merges it.git fetch
only fetches from the remote branch but it does not mergei.e. git pull = git fetch + git merge ...
Trying to be clear and simple.
The git pull command is actually a
shortcut
for git fetch followed by the git merge or the git rebase command depending on your configuration. You can configure your Git repository so that git pull is a fetch followed by a rebase.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
andgit merge
...We simply say:
If you run
git pull
, you do not need to merge the data to local. If you rungit fetch
, it means you must rungit merge
for getting the latest code to your local machine. Otherwise, the local machine code would not be changed without merge.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.
Actually Git maintains a copy of your own code and the remote repository.
The command
git fetch
makes your local copy up to date by getting data from remote repository. The reason we need this is because somebody else might have made some changes to the code and you want to keep yourself updated.The command
git pull
brings the changes in the remote repository to where you keep your own code. Normally,git pull
does this by doing a ‘git fetch’ first to bring the local copy of the remote repository up to date, and then it merges the changes into your own code repository and possibly your working copy.