Now I want to merge a remote branch, naming it origin/branch1
, with my local branch branch1
, as my partner pushed a new commit to branch1 on remote after our last merge, and I haven't committed since his last commit and want to get update from this commit. I used the following commands:
$ git fetch origin branch1
Compressing...
*branch branch1 ->FETCH_HEAD
$ git merge origin/branch1
Already up-to-date
That was not what I intended. What was in my mind before doing this was using fetch to get what my partner added and made the remote branch origin/branch1
being updated. However, the "Already up-to-date" means I failed to get the updates in my local branch1. Then I checked the sha1 value of origin/branch1
by
$ git ls-remote origin
and found that it kept the stale value of old commit after we merged last time. It tells that the git fetch origin branch1
cannot update origin/branch1
. I did another experiment, where my partner created another branch named "branch2" in his side and pushed a commit in branch2 to the remote origin. Then I still used
$ git fetch origin branch2
Compressing...
$ git merge origin/branch2
No branch named "origin/branch2"
The "Compressing" told me that the first command downloaded something in branch2 in origin successfully, however, the second command told me that there was no branch named origin/branch2! Thus I concluded that neither could git fetch origin branchname
update origin/branchname
locally, nor could it create a remote branch if it doesn't exist.
After I replace git fetch origin branch#
with git fetch origin
, all the git merge
s worked as I expected.
However, I often see the combination of
$ git fetch remote branch-name
$ git merge remote/branch-name
So my question is what is the difference between git fetch remote
and git fetch remote branch-name
? And in what conditions could I succeed in letting it work as my wish by this combination?
If you need to know what exactly
git fetch
is doing or any othergit
command, just prefix it withGIT_TRACE=1
, so it'll give you trace output with any other commands which are invoked, e.g.which basically is doing ssh to the remote host, running
git-upload-pack
which send objects packed back togit-fetch-pack
which receives missing objects from another repository.In
man git-upload-pack
we can read:And in
man git-fetch-pack
we can read:To answer the question, the difference between
git fetch remote
andgit fetch remote branch-name
, is that when you don't specify<refspec>
parameter (such as branch), it fetches all branches and/or tags (refs, see:git ls-refs
) from one or more other repositories along with the objects necessary to complete their histories. By default only tags which are reachable by the objects that are fetched are downloaded (e.g. you end up only with tags that point at branches that you are interested in).And when you run with explicit branches and/or tags, git first determines what needs to be fetched, then getting only the relevant refs (branches or tags). E.g.
git fetch origin master
will fetch only the master branch.You can see more about
git fetch-pack
with Git 2.19 (Q3 2018), since "git fetch-pack --all
" used to unnecessarily fail upon seeing an annotated tag that points at an object other than a commit.See commit c12c9df (13 Jun 2018) by Kirill Smelkov (
navytux
).Helped-by: Junio C Hamano (
gitster
).See commit e9502c0 (11 Jun 2018) by Jeff King (
peff
).Helped-by: Junio C Hamano (
gitster
).(Merged by Junio C Hamano --
gitster
-- in commit 0079732, 28 Jun 2018)