Search code examples
gitgit-remote

Differences in "git branch" output after removing and adding again remote repository


When I clone some project from remote reopsitory, and then make git branch -a.

I got:

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

Then, if I remove remote repository and add it again:

$ git remote remove origin
$ git remote add origin username@server:/path/to/repository.git
$ git branch -u origin/master master

I expect something similar, but got different output:

$ git branch -a
* master
  remotes/origin/master

Where this difference comes from, and what does it means? And how can I achieve same output after manual remote addition?

I expect then to see some difference in cat .git/config but outputs are same in both cases.


Solution

  • remotes/origin/HEAD is pretty much like symbolic link (it literally was a symlink in the past, but they had to change it). When you git clone a repository, the branch linked too HEAD will be checked out.

    Github and other such services usually provide an admin UI for modifying this HEAD link so you can set a branch other than master as the default checkout branch. But you can also do it via command line using this command.

    git remote set-head origin master

    That will restore your git branch -a output to how it was when you cloned.

    You can also set up other symbolic references such as foo

    git symbolic-ref refs/remotes/origin/foo refs/remotes/origin/master

    $ git branch -a
    * master
      remotes/origin/HEAD -> origin/master
      remotes/origin/foo -> origin/master
      remotes/origin/master
    

    I've never seen sym-refs in use other than HEAD, but i could imagine a good few scenarios where this could be handy (i.e. if you had a build system that auto-deployed to a test lab of devices, having just a ref your dev team moves around would be good)