8

I am trying to get a remote branch and track it but when I do:

git checkout remotes/mybranch

When I do git branch -a it displays remotes/mybranch in the red color? I get an error:

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

How can I checkout to this mybranch locally and track it? How can I avoid the detached HEAD state?

Pindakaas
  • 4,389
  • 16
  • 48
  • 83

4 Answers4

9

what do I do next time to avoid this detached state?

See "Why did my Git repo enter a detached HEAD state?"

You simply have to checkout a local named branched, as opposed to a remote tracking one (like origin/mybranch).
The remote tracking branch origin/mybranch isn't made to record commits, but only to track the commits fetched from the upstream repo.

To avoid that:

git checkout -b abranch origin/abranch 

Or rather, since Git 2.23 (Q3 2019), using git switch:

git switch abranch

That will will create the local branch "abranch" and track origin/abranch.

See "Difference between git checkout --track origin/branch and git checkout -b branch origin/branch"

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Make a temporary branch then point master to it to reconcile with the detached HEAD like so:

git checkout -b temporary_branch
git branch -f master temporary_branch
git checkout master

Then you can do what you are trying to do:

git checkout remotes/mybranch
ifma
  • 3,673
  • 4
  • 26
  • 38
  • what do I do next time to avoid this detached state? – Pindakaas Jul 23 '15 at 02:05
  • Were you trying to do git rebase? If not, you likely switched to a previous commit and so detached your HEAD. This guy explains it pretty well I think: http://stackoverflow.com/questions/10228760/fix-a-git-detached-head – ifma Jul 23 '15 at 02:17
0

To create a new tracking branch, you can just do this:

git checkout mybranch

This will automatically create a local tracking branch and check it out. It will fail if there are multiple remotes which have a branch named mybranch.

You can rescue work you've done in detached head, but if you haven't done any work in the detached HEAD it is easier just to run the above command.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
0

How can I avoid the detached HEAD state?

Detached HEAD happens when your HEAD points to a specific commit instead to a branch:

enter image description here

By using git checkout with a specific hash, you can decide either to point to a specific commit or to a branch.

The command git switch (added in Git v2.23) was added to only work at branch level (and not at commit level), so a good way to avoid detached heads, is to use git switch instead of git checkout to switch between branches, so this will prevent you to make your HEAD point to a commit unintentionally.

Alex Blasco
  • 793
  • 11
  • 22
  • 1
    Yes, I explained last year why `git switch` was better, in "[Why did my Git repo enter a detached HEAD state?](https://stackoverflow.com/a/3965714/6309)" – VonC Jan 20 '22 at 21:37