git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Bug: `git remote show <remote>` reports different HEAD branch than refs/remotes/<remote>/HEAD

From
IDIgor Djordjevic <igor.d.djordjevic@gmail.com>
Date
Aug 15, 2017, 17:09 UTC
Message-ID
<bad7dafc-247c-bf5e-8cfd-5445badeef1c@gmail.com>
In-Reply-To
<CAKNmmv3_K4gB6FbcmAjXyviMbU2Ts7Rh7txkOof+-36rF_1+Gw@mail.gmail.com>
Hi Jason,
On 15/08/2017 16:26, Jason Karns wrote:
Show 46 quoted lines
> I have a git repo that shows a different branch in
> `.git/refs/remotes/origin/HEAD` than is reported by `git remote show
> origin`.
> 
> The branch is `github-rename` in refs/remotes/origin/HEAD, but shows
> `master` in output of git-remote-show
> 
> ```
> $ cat .git/refs/remotes/origin/HEAD
> ref: refs/remotes/origin/github-rename
> 
> $ git remote show origin
> * remote origin
>   Fetch URL: git@XXXX.git
>   Push  URL: git@XXXX.git
>   HEAD branch: master
>   Remote branches:
>     github-rename     tracked
>     master            tracked
>     qa                tracked
>     refactor-test     tracked
>   Local branches configured for 'git pull':
>     github-rename merges with remote github-rename
>     master        merges with remote master
>   Local refs configured for 'git push':
>     github-rename pushes to github-rename (up to date)
>     master        pushes to master        (up to date)
> ```
> 
> git version 2.14.1
> 
> 
> Background:
> 
> Prior to my repo being cloned, the default branch was configured to be
> `some-random-branch` on github. My repo was cloned and the HEAD branch
> was set to `some-random-branch` correctly (in `refs/`). However,
> git-remote-show reported `master` as the HEAD branch.
> 
> Later, `some-random-branch` was deleted from the remote. It _remained_
> as the HEAD branch locally according to `refs/`.
> 
> In order to test the remote-show command, I changed the HEAD branch to
> a branch that actually existed by running `git remote set-head origin
> github-rename`. It changed the HEAD branch in `refs/` but remote-show
> continues to report `master` as the remote's HEAD. 

I am no expert here, but reading the docs, it seems like you may have wrong expectations.

Documentation for "git remote set-head"[1] explains that this command is used to set default remote branch (locally), where later you can use remote name only to specify that specific (remote) branch instead.

Example shows that for remote named "origin", if you set default branch name to "master" (actually being "origin/master" locally), then whenever you want to type "origin/master", you can type "origin" only instead (set default branch name is implied).

For the given example, that is what you can see inside "refs/remotes/origin/HEAD", being set to "refs/remotes/origin/master".

So it is something _you_ set _locally_ to aid you in working with the remote repository.

On the other hand, what "git remote show" outputs for HEAD is a name of actually checked-out branch inside that remote repository - it`s what`s stored inside HEAD file of the remote repository root.

So it is something set on the _remote_ end, you can`t influence it from your local repository.

What you _could_ do in your specific case, as you mention using GitHub, is following their help page for "setting the default branch"[2] for your GitHub repository (which you track locally as "origin") to "github-rename".

(in general, non-GitHub repository case, one could usually run there either `git checkout github-rename`, if it`s not a bare repository, or `git symbolic-ref HEAD refs/heads/github-rename`, if it`s a bare repository)

Afterwards, running `git remote show origin` inside your local repository should output "github-rename" as HEAD value, as desired.

p.s. To set your default remote branch locally to checked-out branch on the remote end automatically, you can use `git remote set-head origin --auto`, as documented[1]. It will inspect what`s inside "HEAD" of the remote named "origin", and update your local "refs/remotes/origin/HEAD" accordingly.

[1] https://git-scm.com/docs/git-remote#git-remote-emset-headem [2] https://help.github.com/articles/setting-the-default-branch/

Regards, Buga

Previous: Jason KarnsNext: Jason Karns
Message 2 of 4 in “Bug: `git remote show <remote>` reports different HEAD branch than refs/remotes/<remote>/HEAD”
  1. Jason KarnsAug 15, 2017
  2. Igor DjordjevicAug 15, 2017
  3. Jason KarnsAug 15, 2017
  4. Jeff KingAug 15, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.