Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update the HEAD branch in a mirrored clone?

Tags:

git

github

I have a local repository which I cloned through git clone --mirror <ssh-url>. I then keep it up to date using git remote update --prune.

At this point, HEAD points to refs/heads/master

I then go to the Admin section of my github repository, and change the default branch. All of my branches are updated as normally, but HEAD is still refs/heads/master (yes, the branches have different hashes)

My current thought is to use git ls-remote to get the hash of HEAD and all branches, then with some grep/awk magic, extract the hash of HEAD and then choose the first branch with a matching hash, and use git symbolic-ref HEAD <found branch name> to set it locally.

But is there a simpler way to get the remote HEAD branch name (in a manner that it can be updated in a script)?

like image 456
Charlie Avatar asked Mar 05 '13 15:03

Charlie


People also ask

Does cloning pull all branches?

The idea is to use the git-clone to clone the repository. This will automatically fetch all the branches and tags in the cloned repository. To check out the specific branch, you can use the git-checkout command to create a local tracking branch.


1 Answers

Yes, there is a command for this:

git remote set-head origin -a

From git help remote:

With -a, the remote is queried to determine its HEAD, then the symbolic-ref refs/remotes/<name>/HEAD is set to the same branch. e.g., if the remote HEAD is pointed at next, "git remote set-head origin -a" will set the symbolic-ref refs/remotes/origin/HEAD to refs/remotes/origin/next. This will only work if refs/remotes/origin/next already exists; if not it must be fetched first.

like image 115
Chronial Avatar answered Oct 02 '22 17:10

Chronial