Git: Correct way to change Active Branch in a bare repository?
GitGit Problem Overview
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.
Git Solutions
Solution 1 - Git
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 asrefs/remotes/<name>/HEAD
-
doesn't change
HEAD
itself (again, onlyrefs/remotes/<name>/HEAD
), hence the need forgit 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.
Solution 2 - Git
To change the branch you need to change HEAD reference to the branch you want to use.
First list all the references in the bare repository by doing
$find ref
Then find the reference for your branch, the format will be as follows refs/heads/<my_branch>
. So next step is to check current reference, just type:
$git symbolic-ref HEAD
so you know which is the current branch then update it as needed.
$git symbolic-ref HEAD refs/heads/<my_branch>
That's it. Enjoy.
Solution 3 - Git
How to change the Active Branch properly ?
-
status: git checkout
in the repo .git directory returns fatal: This operation must be run in a work tree -
tips: just add the --work-tree argument
detailed example : assumptions: bare git on remote server: > ~/bare_git_repository.git detached work tree: > /var/www/myappremote
on local server: create branch version.1.7 (our otherbranch)
git branch version.1.7
git push origin version.1.7
on the remote server with git bare repo:
$ cd ~/bare_git_repository.git
$ git branch
- master
version.1.7
As stated,following command
git checkout version.1.7
return
fatal: This operation must be run in a work tree
Using the following command
> git --work-tree=/var/www/myappremote checkout version.1.7
successfully change the Active Branch propely
$ git branch
master
- version.1.7
check the results with the following
> ll /var/www/myappremote
hope it will help
Solution 4 - Git
Also, if you don't have access to the bare repository, by doing a git remote set-head
and you are done
See this previous response
Solution 5 - Git
I also have a bare repo on our server and was able to successfully retrieve files using
git clone //server/repo/directory -b branch_name
into a new local repository even though the manpage says this is only for non-bare repositories.
Solution 6 - Git
I compared two directories before and after applying
git symbolic-ref HEAD refs/heads/mybranch
and it appears that only repo.git/HEAD file was changed so probably it is quite safe just to "hack" the file.