How to update HEAD branch in mirror clone?

I have a local repository that I cloned via git clone --mirror <ssh-url> . Then I update it with git remote update --prune .

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

Then go to the Admin section of my github repository and change the default branch. All my branches are updated as usual, 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 HEAD hash and all branches, then using some grep / awk magic, extract the HEAD hash, then select the first branch with the corresponding hash and use git symbolic-ref HEAD <found branch name> to install it locally.

But is there an easier way to get the branch name of the remote HEAD server (so that it can be updated in the script)?

+4
source share
3 answers

Yes, there is a command for this:

 git remote set-head origin -a 

From git help remote :

With -a, the console is requested to determine its HEAD, then symbolic-ref refs / remotes // HEAD is installed in one branch. for example, if the remote HEAD is specified as follows: "git" remote start -a "will set the symbolic ref refs / remotes / origin / HEAD for the link / remote controls / origin / next. This will only work if refs / remotes / origin / next already exists; if it should not be selected first.

+2
source

To request a remote HEAD, use

 git remote show origin * remote origin Fetch URL: git://... Push URL: git://... HEAD branch: whateverremotehead Remote branches: ... 

and parse the output looking for the "HEAD branch" and use this in

 git symbolic-ref HEAD refs/heads/whateverremotehead 

I do not know a better way.

0
source

Since you are using ssh, you can use this command if you have the correct ssh access:

 scp yourusername@yourserver :/path/to/the/server/repo/HEAD local/repo/.git/HEAD 
-1
source

All Articles