Fix origin/HEAD after a default branch rename
After a default branch rename, repair a stale origin/HEAD with git remote set-head or configure fetch to follow the remote.
Your team renamed the default branch on the remote from master to main. You run git fetch --prune and origin/master disappears. Anything that used the bare remote name as a revision stops working: git log origin, git rebase origin, git diff origin....
$ git fetch --prune
- [deleted] (none) -> origin/master
refs/remotes/origin/HEAD has become dangling after refs/remotes/origin/master was deleted
* [new branch] main -> origin/main
$ git symbolic-ref refs/remotes/origin/HEAD
refs/remotes/origin/masterGit names the problem in the fetch output and then leaves it. origin/HEAD is a symbolic ref that git clone wrote once, pointing at whatever the remote's default branch was that day.
It is what lets you write origin on its own where a command wants a revision. git fetch updates the branches under refs/remotes/origin/ and leaves HEAD alone, so after the rename it points at a branch that no longer exists.
Fix one clone
git remote set-head origin --auto
git symbolic-ref refs/remotes/origin/HEADThe first command asks the remote which branch its HEAD points at and repoints origin/HEAD there. The second prints refs/remotes/origin/main, and git log origin works again.
If you know the branch name, git remote set-head origin main does the same without asking the remote. Either way the branch has to exist under refs/remotes/origin/ already, so fetch first.
This fixes the clone you are in. Anyone with another stale clone needs to update theirs.
Let fetch do it
Git 2.48 added remote.<name>.followRemoteHEAD, which tells git fetch how to handle origin/HEAD when fetching the remote with its configured refspec. The default is create: write the ref when it is missing, never touch one that exists. That is the behaviour you saw above.
git config --global remote.origin.followRemoteHEAD always
git fetch origin
git symbolic-ref refs/remotes/origin/HEADWith always, the fetch repoints origin/HEAD to the remote's current default branch without a message, and the last line prints refs/remotes/origin/main. The --global flag applies it to clones under your user account with a remote named origin, unless they override the setting. Omit it to configure just this repository.
The other value worth knowing is warn. Git creates the ref if missing. If it exists and points to a different branch, Git leaves it in place and warns:
'HEAD' at 'origin' is 'main', but we have 'master' locally.
I use always and let Git follow the rename. If you have scripts that read origin/HEAD and want to be told before it moves, warn gives you the line and the choice. never switches the whole thing off, including the create behaviour on a fresh fetch.