What is the difference between pull and fetch in git?
What specifically are the differences between doing a git pull
and a git fetch
?
5 answers
Of the documentation :
git pull
is shorthand forgit fetch
followed bygit merge FETCH_HEAD
.
Or making a free translation:
git pull
it is an abbreviation ofgit fetch
followed bygit merge FETCH_HEAD
.
That is, git fetch
brings the changes, but leaves them in another branch, until the git merge
is done to bring them to the branch local.
Always in a repository you have a hidden branch, which you can see by using git branch -a
.
That hidden branch is origin/master
.
You when using git fetch
, drop the changes from the remote repository to the origin/master
branch:
Git fetch origin
Now you already have the changes in origin/master
, but you would have to pass them to the master
branch, for that you have to use:
Git merge origin / master
From this you have the new changes to your branch master
and you're done.
Here is an image I made to explain this part:
By using git pull
you are combining git fetch
+ git merge
.
Git pull origin master
In conclusion with git pull
you are saving yourself from using one more command, but I recommend that if you are just starting to use git, keep using git fetch
and git merge
Actually git pull
downloads the changes of the given branch and updates it against your local repository.
git fetch
drop the changes of the given branch and place it in a mirror Branch which is simply a hidden branch class in which you can look at the changes of that branch, and then merge with your local branch.
The git pull
is simply a git fecth + git merge
. Do not use the git pull
if you are actually doubtful what changes can be made. from the remote repository.
When you do a git fetch
, the changes from your remote repository(if any) will be downloaded to a folder called origin/master, which is a hidden folder. To include the changes to your local branch you need to merge master with origin / master.
Git pull
it does all that automatically.
Very short: with fetch you only query the changes that are in the repository with respect to your local copy; and with pull you lower the changes to your local. I think Sourcetree when doing pull, does a fetch first as well.