kbro kbro - 1 month ago 15
Git Question

Git: Correct way to change Active Branch in a bare repository?

I have a bare repository that's used as the central store for my project. All the developers do

git clone <repo>
to share with it. When they do the clone, they get a checkout of the master branch (unless they do
git clone -n
) because
repo.git/HEAD
contains
ref: refs/heads/master
, making this the Active Branch.

The question is, how do I change the Active Branch properly? I could simply hack the
repo.git/HEAD
file directly, but that seems nasty and, well, hacky.

I tried doing
git checkout <otherbranch>
in the repo
.git
directory, but that failed because I wasn't in a work tree.

I tried
git update-ref HEAD refs/heads/otherbranch
but that just updated refs/heads/master to be the same as refs/heads/otherbranch (okay, I did that one in a dummy repository, not my production one!)

I tried
git update-ref --no-deref HEAD refs/heads/otherbranch
and that almost worked. It updated the
HEAD
file, but it set it to the SHA1 of the commit pointed to by
refs/heads/otherbranch
.

I'm testing with git version
1.7.0.2.msysgit.0
.

I'm guessing there's no way to do this through
git push
, as allowing all and sundry to change your default branch seems a bit unsafe (!), but surely there's a better way to do it in the repo
.git
directory than directly hacking the
HEAD
file.

Answer

If you have access to the remote bare repo, this article suggests:

git symbolic-ref HEAD refs/heads/mybranch

Which will update the HEAD file in your repository so that it contains:

ref: refs/heads/mybranch

as documented in the git-symbolic-ref


If you don't have access to the remote repo, see my previous answer.


Remember that a command like git remote set-head:

  • doesn't change the default branch of the remote repo.
    It only changes a remote tracking branch stored in your local repo as refs/remotes/<name>/HEAD

  • doesn't change HEAD itself (again, only refs/remotes/<name>/HEAD), hence the need for git symbolic-ref.

So git remote set-head is not the answer here.
git symbolic-ref HEAD is, if you have direct access to the remote repo.

Comments