Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

People also ask

What is git bare repository?

What is a bare repository? A bare repository is the same as default, but no commits can be made in a bare repository. The changes made in projects cannot be tracked by a bare repository as it doesn't have a working tree. A working tree is a directory in which all the project files/sub-directories reside.


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.


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.


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