219

I have a branch set up to track a ref in origin. git checkout <branchname> switches to that branch, and a git status will show me how far ahead or behind my branch is from origin, but I'm surprised that origin/HEAD still points at origin/master, and not origin/<branchname>

So my question is, under what circumstances does origin/HEAD get moved?

EDIT:

I appreciate the answers about how to move origin/HEAD, but I'm interested in what "organically" moves it, outside of me explicitly telling it to do so.

For example, when I switch branches, git makes HEAD point at the branch I'm checking out, so I'm surprised that origin/HEAD doesn't move in the same manner.

1
  • Note that this question is about local symbolic references on remotes, like refs/origin/HEAD. It is not about how a repository's own symbolic reference HEAD gets set.
    – clacke
    Commented Mar 28, 2017 at 16:53

7 Answers 7

257

Note first that your question shows a bit of misunderstanding. origin/HEAD represents the default branch on the remote, i.e. the HEAD that's in that remote repository you're calling origin. When you switch branches in your repo, you're not affecting that. The same is true for remote branches; you might have master and origin/master in your repo, where origin/master represents a local copy of the master branch in the remote repository.

origin's HEAD will only change if you or someone else actually changes it in the remote repository, which should basically never happen - you want the default branch a public repo to stay constant, on the stable branch (probably master). origin/HEAD is a local ref representing a local copy of the HEAD in the remote repository. (Its full name is refs/remotes/origin/HEAD.)

I think the above answers what you actually wanted to know, but to go ahead and answer the question you explicitly asked... origin/HEAD is set automatically when you clone a repository, and that's about it. Bizarrely, that it's not set by commands like git remote update - I believe the only way it will change is if you manually change it. (By change I mean point to a different branch; obviously the commit it points to changes if that branch changes, which might happen on fetch/pull/remote update.)


Edit: The problem discussed below was corrected in Git 1.8.4.3; see this update.


There is a tiny caveat, though. HEAD is a symbolic ref, pointing to a branch instead of directly to a commit, but the git remote transfer protocols only report commits for refs. So Git knows the SHA1 of the commit pointed to by HEAD and all other refs; it then has to deduce the value of HEAD by finding a branch that points to the same commit. This means that if two branches happen to point there, it's ambiguous. (I believe it picks master if possible, then falls back to first alphabetically.) You'll see this reported in the output of git remote show origin:

$ git remote show origin
* remote origin
  Fetch URL: ...
  Push  URL: ...
  HEAD branch (remote HEAD is ambiguous, may be one of the following):
    foo
    master

Oddly, although the notion of HEAD printed this way will change if things change on the remote (e.g. if foo is removed), it doesn't actually update refs/remotes/origin/HEAD. This can lead to really odd situations. Say that in the above example origin/HEAD actually pointed to foo, and origin's foo branch was then removed. We can then do this:

$ git remote show origin
...
HEAD branch: master
$ git symbolic-ref refs/remotes/origin/HEAD
refs/remotes/origin/foo
$ git remote update --prune origin
Fetching origin
 x [deleted]         (none)     -> origin/foo
   (refs/remotes/origin/HEAD has become dangling)

So even though remote show knows HEAD is master, it doesn't update anything. The stale foo branch is correctly pruned, and HEAD becomes dangling (pointing to a nonexistent branch), and it still doesn't update it to point to master. If you want to fix this, use git remote set-head origin -a, which automatically determines origin's HEAD as above, and then actually sets origin/HEAD to point to the appropriate remote branch.

8
  • @jefromi Awesome answer! Just a remark: you write that HEAD is a symbolic ref, pointing to a branch instead of directly to a commit [...], but it might be worth mentioning "detached HEAD state", for completeness.
    – jub0bs
    Commented Aug 21, 2014 at 13:26
  • 2
    @Jubobs Thanks! If my answer needs updating, please feel free to simply edit it, though - it'll certainly save people time to read a brief summary of how things actually work, rather than having to sort through what was true two years ago and what's true now.
    – Cascabel
    Commented Aug 21, 2014 at 18:35
  • 2
    have read this at least 5 times and still do no understand a bit of it
    – krb686
    Commented Feb 26, 2015 at 15:19
  • 35
    git remote set-head origin -a did the job for me
    – Shujito
    Commented May 29, 2018 at 19:09
  • 4
    BTW: for whatever reason, a lot of repos are changing HEAD in remote repos (normally to "main"). The purported reason seems to be the claim that some people find "master" offensive. (FWIW I'd give that explanation more credence if the people giving it seem to be offended themselves rather than be offended on behalf of someone else. But whatever, it's their repo.)
    – BCS
    Commented Nov 17, 2021 at 3:51
122

It is your setting as the owner of your local repo. Change it like this:

git remote set-head origin some_branch

And origin/HEAD will point to your branch instead of master. This would then apply to your repo only and not for others. By default, it will point to master, unless something else has been configured on the remote repo.

Manual entry for remote set-head provides some good information on this.

Edit: to emphasize: without you telling it to, the only way it would "move" would be a case like renaming the master branch, which I don't think is considered "organic". So, I would say organically it does not move.

2
  • 1
    The edit emphasis isn't completely correct here. It can also change if you clone from a local copy that isn't on master branch.
    – mphair
    Commented May 15, 2014 at 7:13
  • I don't consider a clone "moving", but I guess we can disagree on that :)
    – eis
    Commented Sep 1, 2017 at 7:06
45

What moves origin/HEAD "organically"?

  • git clone sets it once to the spot where HEAD is on origin
    • it serves as the default branch to checkout after cloning with git clone

What does HEAD on origin represent?

  • on bare repositories (often repositories “on servers”) it serves as a marker for the default branch, because git clone uses it in such a way
  • on non-bare repositories (local or remote), it reflects the repository’s current checkout

What sets origin/HEAD?

  • git clone fetches and sets it
  • it would make sense if git fetch updates it like any other reference, but it doesn’t
  • git remote set-head origin -a fetches and sets it
    • useful to update the local knowledge of what remote considers the “default branch”

Trivia

  • origin/HEAD can also be set to any other value without contacting the remote: git remote set-head origin <branch>
    • I see no use-case for this, except for testing
  • unfortunately nothing is able to set HEAD on the remote
  • older versions of git did not know which branch HEAD points to on the remote, only which commit hash it finally has: so it just hopefully picked a branch name pointing to the same hash
  • see @MichaWiedenmann's comment: origin/HEAD is used as short-cut to a branch, when you name a remote only
3
  • I had lost reference to origin/HEAD and your solution helped. Thanks!
    – java_dude
    Commented Apr 5, 2015 at 23:32
  • I disagree with git fetch updating it, since it allows to configure a (local) shortcut. Quoting the doc: "Having a default branch for a remote is not required, but allows the name of the remote to be specified in lieu of a specific branch". It would be strange if a remote change would update locally configured shortcuts. Commented Nov 21, 2018 at 7:51
  • 1
    @MichaWiedenmann Why would that be a locally configured shortcut? For a locally configured shortcut origin/HEAD is a bad name. And that git clone uses a remote name as a default for a “locally configured branch” contradicts that as well. On non-bare repositories it does not even make sense to use remote’s current HEAD. Commented Jun 1, 2020 at 17:55
16

Disclaimer: this is an update to Cascabel's answer, which I'm writing to save the curious some time.

I tried in vain to replicate (in Git 2.0.1) the remote HEAD is ambiguous message that Cascabel mentions in his answer; so I did a bit of digging (by cloning https://github.com/git/git and searching the log). It used to be that

Determining HEAD is ambiguous since it is done by comparing SHA1s.

In the case of multiple matches we return refs/heads/master if it
matches, else we return the first match we encounter. builtin-remote
needs all matches returned to it, so add a flag for it to request such.

(Commit 4229f1fa325870d6b24fe2a4c7d2ed5f14c6f771, dated Feb 27, 2009, found with git log --reverse --grep="HEAD is ambiguous")

However, the ambiguity in question has since been lifted:

One long-standing flaw in the pack transfer protocol used by "git
clone" was that there was no way to tell the other end which branch
"HEAD" points at, and the receiving end needed to guess.  A new
capability has been defined in the pack protocol to convey this
information so that cloning from a repository with more than one
branches pointing at the same commit where the HEAD is at now
reliably sets the initial branch in the resulting repository.

(Commit 9196a2f8bd46d36a285bdfa03b4540ed3f01f671, dated Nov 8, 2013, found with git log --grep="ambiguous" --grep="HEAD" --all-match)

Edit (thanks to torek):

$ git name-rev --name-only 9196a2f8bd46d36a285bdfa03b4540ed3f01f671
tags/v1.8.4.3~3

This means that, if you're using Git v1.8.4.3 or later, you shouldn't run into any ambiguous-remote-HEAD problem.

0
10

Remember there are two independent git repos we are talking about. Your local repo with your code and the remote running somewhere else.

Your are right, when you change a branch, HEAD points to your current branch. All of this is happening on your local git repo. Not the remote repo, which could be owned by another developer, or siting on a sever in your office, or github, or another directory on the filesystem, or etc...

Your computer (local repo) has no business changing the HEAD pointer on the remote git repo. It could be owned by a different developer for example.

One more thing, what your computer calls origin/XXX is your computer's understanding of the state of the remote at the time of the last fetch.

So what would "organically" update origin/HEAD? It would be activity on the remote git repo. Not your local repo.

People have mentioned

git symbolic-ref HEAD refs/head/my_other_branch

Normally, that is used when there is a shared central git repo on a server for use by the development team. It would be a command executed on the remote computer. You would see this as activity on the remote git repo.

2
  • 1
    Sorry, if I'm a little repetitive. I just want to point out the fact that git is a distributed version control system, and as such the two repos are independent. Commented Jan 12, 2012 at 19:25
  • 'git symbolic-ref HEAD refs/head/my_other_branch' This is what I needed! That way I could rename my remote master to main and after that delete the reference with 'git push origin -d master' Commented Feb 2, 2023 at 19:38
2

Run the following commands from git CLI:

# move to the wanted commit
git reset --hard <commit-hash> 

# update remote
git push --force origin <branch-name> 
1
  • That pushes the branch with it tied to the desired commitish, but that doesn't actually change the remote HEAD. Commented Jan 1, 2021 at 23:45
1

It gets set by git clone and also by git remote add -m master $url. Other than that you would have to set it manually.

I did sent some patches to be updated with git fetch as well (depending on some configuration). That would be the most organic way to update it in my opinion. Unfortunately they haven't been merged yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.