79

I don't understand the second line in the output to git branch -l -a: remotes/origin/HEAD -> origin/master.

git branch -l -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

Is that a leftover from another operation? Should I clean it up? And how would I do that?

Usually I work with git on the cli, but on this local repository I experimented with TortoiseGit to find an easy git workflow for a friend.

mistaecko
  • 2,675
  • 1
  • 20
  • 18
  • 1
    possible duplicate of [Why is "origin/HEAD" shown when running "git branch -r"?](http://stackoverflow.com/questions/354312/why-is-origin-head-shown-when-running-git-branch-r) – Colin D Bennett Sep 11 '13 at 16:06
  • 1
    By the way, `-l` probably does not do what you want. Generally, [it creates reflog](https://git-scm.com/docs/git-branch#git-branch--l), but here it is probably just omitted. For listing all branches, `git branch -a` is enough. – Palec Jun 05 '17 at 10:13
  • @ColinDBennett No. – JobHunter69 Aug 18 '18 at 22:46

2 Answers2

50

No, no need to clean up: it is the symbolic branch referenced by your remote repo.
When you clone your repo, you will be by default on the branch referenced by remotes/origin/HEAD.

See also:

Note: on Git versions older than 2.20, you need to use git branch --list (or git branch), not git branch -l.

Dario Seidl
  • 4,140
  • 1
  • 39
  • 55
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    Thx for the concise answer and the links. I guess I was confused because I compared this test repo to another one that didn't have the remotes/origin/HEAD reference. That other repo was the original repo that I pushed to github, hence had never been cloned. Is it correct that this (not having been cloned) is the reason that it doesn't contain the HEAD reference? – mistaecko Sep 27 '12 at 07:19
  • 5
    If you do want to remove it: `git remote set-head origin -d`, per http://stackoverflow.com/a/6838756. – G-Wiz Jun 19 '13 at 17:24
  • What's the difference between second branch and third branch? – JobHunter69 Aug 18 '18 at 22:49
  • @Goldname The second branch is not a branch, but a symbolic pointer (HEAD) to a branch. It references the default branch that will be checked out when you are cloning the remote repo. – VonC Aug 18 '18 at 22:51
  • @VonC Ok so the second line tells you of the pointer to a branch and what the branch is, right? And the third line lists the branch – JobHunter69 Aug 18 '18 at 22:54
  • @Goldname Yes, pointer (in the '`remotes`' namespace) and then branch (in the '`remotes`' namespace): they are copies of what was last fetched from the remote repo named `origin`. – VonC Aug 18 '18 at 23:01
  • @VonC Thanks, but I'm getting more confused. "copies of what was last fetched" How is a list of branches related to fetching? – JobHunter69 Aug 19 '18 at 01:40
  • 1
    @Goldname Because a branch is a pointer to the most recent commit, itself referencing to the history of past commits. Whenever you fetch, you are getting all new commits, and updating remotes/origin/master to the most recent of those new commits. To know more about branches: https://stackoverflow.com/a/51224861/6309, about fetching: https://stackoverflow.com/a/28341622/6309, https://stackoverflow.com/a/23530333/6309 – VonC Aug 19 '18 at 03:45
41

You can use git remote set-head origin -d to delete the origin/HEAD symbolic ref, or git remote set-head origin -a to query the remote and automatically set the origin/HEAD pointer to the remote's current branch.

The origin/HEAD reference is optional. It only acts as a syntactic shortcut: If it exists and points to origin/master, you can use specific simply origin where you would otherwise specify origin/master.

The git remote(1) man page describes this:

set-head

Sets or deletes the default branch (i.e. the target of the symbolic-ref refs/remotes//HEAD) for the named remote. 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. For example, if the default branch for origin is set to master, then origin may be specified wherever you would normally specify origin/master.

Colin D Bennett
  • 11,294
  • 5
  • 49
  • 66