Difference between git merge and git fetch?
UPDATE . Good grief, my merge diagrams have been wrong this whole time. The merge does not move the "other" ref branch ...
git fetch
is to retrieve data from a remote repository.
git merge
is to merge work with multiple lines of work (usually local branches, but see below).
git pull
(I know you didn't ask about pull
, but bear with me) is a shorthand that fetches data from a remote, for example fetch
, and then merge
into your current branch the appropriate line of work from the remote (if there is one, "tracking info" defines this.)
So, let's say you have a remote repo with one branch ( master
) containing 5 commits.
'origin' repo
A --- B --- C --- D --- E <--(master)
You cloned this repo a while ago; at that time it only has the first commit ( A
). Then you created a new branch ( branch1
) and did a little work creating a new commit ( L
) on that branch. Finally, you've changed the changes from the remote; more on how this works later, but for now let's just say you updated yours master
to include B
.
local repo
A --- B <--(master)(origin/master)
\
L <-- (branch1)
Note that in addition to your local branch ( master
and branch1
) replicas , you have a remote branch reference ( origin/master
) which appears to be the same as master
.
Now if you wanted to update your local repo to contain all data from source, but without merging anything, you would say
git fetch
and then you
C --- D --- E <--(origin/master)
/
A --- B <--(master)
\
L <-- (branch1)
This is a sample - just get the data from the remote.
The main reason you explicitly requested merge
is to merge your work branch1
with yours master
. So
git checkout master
git merge branch1
(then maybe resolve any conflicts) and now you have
C --- D --- E <--(origin/master)
/
A --- B --- M <--(master)
\ /
L ------- <--(branch1)
(In some circumstances, when only one branch contains changes that are not in the other, the merge can be done using "fast forward", but this does not apply here, since each branch had changes, that is, the branches were scattered. there is another technique called reinstallation that can sometimes be used to merge branches, but that could be another of the worms ...)
So the difference between fetch
and merge
is very different operations that do different things. But I also mentioned pull
which combines the two. If you do pull
, it first fetches the changes from the remote (in case you are not fully updated with fetch
) and then, if the current branch has a matching remote branch, it merges them.
# still on master
git pull
gives something like
C --- D --- E --- N <--(master)(origin/master)
/ /
A --- B --------------- M
\ /
L ------------------- <--(branch1)
(Note that although I usually draw these diagrams so that the "straight line" entering the merge is the "first parent", in this case it became a hassle for N
, but it shows the general accomplishment of the topology ...)
Back when I talked about "pushing changes" to get B
into your local repo, most likely it would have been done withgit pull
source to share