202

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.

ecoffey
  • 3,423
  • 4
  • 23
  • 22
  • 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 Mar 28 '17 at 16:53

7 Answers7

241

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.

Community
  • 1
  • 1
Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • @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 Aug 21 '14 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 Aug 21 '14 at 18:35
  • 1
    have read this at least 5 times and still do no understand a bit of it – krb686 Feb 26 '15 at 15:19
  • 30
    `git remote set-head origin -a` did the job for me – Shujito May 29 '18 at 19:09
  • 3
    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 Nov 17 '21 at 03:51
  • Is there a way to track how/when head was changed from master to main – awm Dec 07 '22 at 04:00
  • @awm It depends. If you want to know when the "default branch" was changed, and the change took place by branching from main and then changing the default branch setting in your repo provider settings (Gitlab project settings, for ex), then you can look at the git commit history to see when master stopped getting updates (if it wasn't deleted). If you want to see when the symbolic ref for origin/HEAD was changed on your git server; that's a local client symbolic ref so I think its not currently something that you can identify from git commands and you would need shell access to the git server. – Speeddymon Apr 15 '23 at 03:05
113

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.

eis
  • 51,991
  • 13
  • 150
  • 199
  • 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 May 15 '14 at 07:13
  • I don't consider a clone "moving", but I guess we can disagree on that :) – eis Sep 01 '17 at 07:06
38

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
Robert Siemer
  • 32,405
  • 11
  • 84
  • 94
  • I had lost reference to `origin/HEAD` and your solution helped. Thanks! – java_dude Apr 05 '15 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. – Micha Wiedenmann Nov 21 '18 at 07: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`. – Robert Siemer Jun 01 '20 at 17:55
13

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.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • 1
    Based on the tags in the git source, this fix applies to git version 1.8.4.3 and later. – torek Aug 21 '14 at 18:08
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.

Pablo Maurin
  • 1,462
  • 1
  • 22
  • 33
  • 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. – Pablo Maurin Jan 12 '12 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' – 00prometheus Feb 02 '23 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> 
Yakir GIladi Edry
  • 2,511
  • 2
  • 17
  • 16
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.

FelipeC
  • 9,123
  • 4
  • 44
  • 38