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
?
Git obtains the branch of the latest version from the remote to the local using two commands:
git fetch: Git is going to get the latest version from remote to local, but it do not automatically merge.
git fetch origin master
git log -p master..origin/master
git merge origin/master
The commands above mean that download latest version of the main branch from origin from the remote to origin master branch. And then compares the local master branch and origin master branch. Finally, merge.
git pull: Git is going to get the latest version from the remote and merge into the local.
git pull origin master
The command above is the equivalent to
git fetch
andgit merge
. In practice,git fetch
maybe more secure because before the merge we can see the changes and decide whether to merge.Git Fetch
You download changes to your local branch from origin through fetch. Fetch asks the remote repo for all commits that others have made but you don't have on your local repo. Fetch downloads these commits and adds them to the local repository.
Git Merge
You can apply changes downloaded through fetch using the merge command. Merge will take the commits retrieved from fetch and try to add them to your local branch. The merge will keep the commit history of your local changes so that when you share your branch with push, Git will know how others can merge your changes.
Git Pull
Fetch and merge run together often enough that a command that combines the two, pull, was created. Pull does a fetch and then a merge to add the downloaded commits into your local branch.
It cost me a little bit to understand what was the difference, but this is a simple explanation.
master
in your localhost is a branch.When you clone a repository you fetch the entire repository to you local host. This means that at that time you have an origin/master pointer to
HEAD
and master pointing to the sameHEAD
.when you start working and do commits you advance the master pointer to
HEAD
+ your commits. But the origin/master pointer is still pointing to what it was when you cloned.So the difference will be:
git fetch
it will just fetch all the changes in the remote repository (GitHub) and move the origin/master pointer toHEAD
. Meanwhile your local branch master will keep pointing to where it has.git pull
, it will do basically fetch (as explained previously) and merge any new changes to your master branch and move the pointer toHEAD
.git fetch
pulls down the code from the remote server to your tracking branches in your local repository. If your remote is namedorigin
(the default) then these branches will be withinorigin/
, for exampleorigin/master
,origin/mybranch-123
, etc. These are not your current branches, they are local copies of those branches from the server.git pull
does agit fetch
but then also merges the code from the tracking branch into your current local version of that branch. If you're not ready for that changes yet, justgit fetch
first.git pull == ( git fetch + git merge)
git fetch does not changes to local branches.
If you already have a local repository with a remote set up for the desired project, you can grab all branches and tags for the existing remote using git fetch . ... Fetch does not make any changes to local branches, so you will need to merge a remote branch with a paired local branch to incorporate newly fetch changes. from github
OK, here are some information about
git pull
andgit fetch
, so you can understand the actual differences... in few simple words, fetch gets the latest data, but not the code changes and not going to mess with your current local branch code, but pull get the code changes and merge it your local branch, read on to get more details about each:git fetch
It will download all refs and objects and any new branches to your local Repository...
git pull
It will apply the changes from remote to the current branch in local...
I also create the visual below to show you how
git fetch
andgit pull
working together...