1

If I am working on a branch of a repo, say devel, what is the difference between the following commands? In this case, I have already added an upstream remote:

$ git fetch upstream devel
From github.com:meteor/meteor
 * branch            devel      -> FETCH_HEAD

and

$ git fetch upstream 
remote: Counting objects: 500, done.
remote: Compressing objects: 100% (230/230), done.
remote: Total 326 (delta 227), reused 187 (delta 90)
Receiving objects: 100% (326/326), 46.61 KiB, done.
Resolving deltas: 100% (227/227), completed with 87 local objects.
From github.com:meteor/meteor
 * [new branch]      appconfig  -> upstream/appconfig
 * [new branch]      check-path -> upstream/check-path
   248ff08..4d44a4d  ctl-migrate -> upstream/ctl-migrate
   2c356d2..bfb8fd3  devel      -> upstream/devel
 * [new branch]      release-0.6.5 -> upstream/release-0.6.5
   64b95a4..c0014df  shark      -> upstream/shark
From github.com:meteor/meteor
 * [new tag]         release/0.6.5-rc11 -> release/0.6.5-rc11
 * [new tag]         release/0.6.5-rc12 -> release/0.6.5-rc12

How does fetch know what to get if no branch is specified?

Moreover, why is it that the merge command is

$ git merge upstream/devel

but if I want to push the changes to my fork, I need to say

$ git push origin devel 

why is there a slash in one case but not the other?

Andrew Mao
  • 35,740
  • 23
  • 143
  • 224

1 Answers1

2

git fetch upstream devel is the same than:

 git fetch upstream refs/heads/devel

You don't provide a destination in your refspec, so the fetch update FETCH_HEAD, as explained in "In git, how do I check out a remote repository's remote branches?".
This is a short-lived ref, used if you do a pull (fetch + merge).

git fetch upstream fetches everything, so there is no single ref to update.


git merge must specify the source of the merge, in your case the remote branch devel (upstream/devel since upstream is the name of your updated remote, updated after the fetch)
git merge devel would merge the local branch devel on the current branch... and if you are already on devel, that would result in a no-op.

But git push also specify the source, here a local branch devel, a remote destination origin (the "upstream repo") and the destination depends on the push policy.
If devel is tracking a remote branch, and you currently are on devel, a simple git push is enough. See "Why do I need to explicitly push a new branch?" for more.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Isn't `git fetch upstream devel` more like `get fetch upstream refs/heads/devel` (note the *heads*)? Remote refs should be under the remote name too, so it would be `refs/remote/upstream/devel`, if it were the right ref. – John Szakmeister Aug 03 '13 at 12:28
  • +1 Thanks for the comprehensive explanation and pointers. I'm sure this will be of value to others as well because so far I just type branch arguments randomly and pray that it is doing the right thing :) – Andrew Mao Aug 03 '13 at 16:05