Upstream links disappear when fetch refspecs are provided directly in the command line

47 Views Asked by At

Consider a common scenario where we have two upstream branches set up for two local branches. This can be verified using git branch --all -vv which provides the following output:

* master                     74d2505 [origin/master] commit_message_X
  my_branch1                 55f5728 [origin/my_branch1] commit_message_Y

The corresponding remote-tracking branches are clearly shown above.

Now here is an experiment. I remove the line fetch = +refs/heads/*:refs/remotes/origin/* from .git/config and instead run git fetch origin +refs/heads/*:refs/remotes/origin/* directly in the command line. Then, I re-ran git branch --all -vv only to be surprised by the fact that all the remote-tracking branches within square brackets were gone:

* master                     74d2505 commit_message_X
  my_branch1                 55f5728 commit_message_Y

At the last part of the experiment, I added the line fetch = +refs/heads/*:refs/remotes/origin/* back to .git/config and ran an argument-less fetch git fetch origin. The remote-tracking branches got all back.

* master                     74d2505 [origin/master] commit_message_X
  my_branch1                 55f5728 [origin/my_branch1] commit_message_Y

Can anybody help me understand what is going on here? Why is it important to include fetch refspecs in the .git/config file and not in the command line? How does git exactly establish the remote-tracking branches shown within square brackets in the output of git branch --all -vv?

Just in case, here are some relevant parts of the .git/config file:

[branch "master"]
    remote = origin
    merge = refs/heads/master

[branch "my_branch1"]
    remote = origin
    merge = refs/heads/my_branch1
0

There are 0 best solutions below