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)?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With