2

I am getting into detached state head state all the time. I don't know why and I don't know what it is.

Every time I am trying to recover from this state, I am loosing some files (from last commit I did in detached head state).

Is it possible to avoid this state completely or it is by design?

2
  • 1
    git reflog is your friend.
    – AD7six
    Commented May 29, 2016 at 16:41
  • Avoid git checkout sha1. here sha1 could be HEAD, FETCH_HEAD. And there are some other cases leading to detached HEAD state. If you know exactly which branch you are working with, it's okay to stay at detached HEAD state.
    – ElpieKay
    Commented May 29, 2016 at 16:42

3 Answers 3

5

Detached head state exists in the git by design and cannot normally be turned off. You can get into detached head state only if you, after cloning your repository, do something like this

git checkout $specific_commit

for example if you are at the tip of the named branch master

#1 -> #2 -> #3
            ^
            master
            ^
            HEAD

and you do

git checkout HEAD~

which is commit right before what HEAD currently points to. Then you will end up in the following state

#1 -> #2 -> #3
      ^     ^
      |     master
      |
      HEAD

From this point you are in detached head state, i.e. HEAD points at a specific commit and not at the branch label. Prior to this HEAD was pointing at the branch label master, which in turn was pointing at the specific commit (tip of branch). Now what ever commits you will make they will be on the separate branch that does not have a branch label (i.e. reference). Running git branch will tell you if you in detached head state.

$ git branch
* (detached from 60e425a)
  master

Notice the star *, which indicates your current branch. If you make new commit right now git repo will look like this.

#1 -> #2 -> #3
      |     ^
      |     master
      |
      \----> #5
             ^
             HEAD

if you switch now back to master by doing git checkout master then HEAD will switch master and branch that was created when you created commit #5 will no longer have any label, i.e. a symbolic reference to navigate to commit #5. Which means that your commit is effectively lost.

Hence you have two options.

  1. If you do not want to end up in the detached head state then just do not do git checkout $specific_commit. Always stay at the tip of same named branch such as master. You can verify that by running git branch.
  2. If you do find yourself in the detached head state and perhaps you already made commits that you do not want to "loose" then, as Flows suggested above, you need to create new branch label/reference like so

    git checkout -b myNewBranch

which will make your repository look like so

#1 -> #2 -> #3
      |     ^
      |     master
      |
      \----> #5
             ^
             myNewBranch
             ^
             HEAD

Then if you witch back to master you can always navigate back to commit #5 and all subsequent commits on that branch by doing git checkout myNewBranch.

5

Work only with local branches and then you will never get into the detached state. For example, instead of git checkout origin/master do git checkout master && git pull origin master, see also tracking options for branches.

To avoid loosing changes already sitting in a detached HEAD, create a branch for them: git branch someNameForTheBranch

7
  • At the moment I do git checkout master, I am loosing my changes. I don't want to loose them.
    – Dims
    Commented May 29, 2016 at 16:42
  • @Dims your question was more on how to avoid getting into this state. How to recover is a slightly different story, see the update of my reply. Commented May 29, 2016 at 16:46
  • @Dims no you don't lose them. git reflog lists HEAD's history, in which you could find last detached HEAD.
    – ElpieKay
    Commented May 29, 2016 at 16:57
  • Sure, I need to avoid getting into detached head, but simultaneously I wish not to loose my changes. Command git checkout master is removing my changes, so it is not an option.
    – Dims
    Commented May 29, 2016 at 16:57
  • @ElpieKay once I find them, then what can I do in order to (1) regain them and (2) never have detached head state anymore?
    – Dims
    Commented May 29, 2016 at 16:59
1

If you used to do git checkout sha1, you should add -b option to automatically create a new branch on commit sha1 and moving on it.

The priority is to avoid to be in this state. If you want to correct the detached head situation you can do

git stash
git checkout -b StartingPointOfYourBranch
git stash pop

"StartingPointOfYourBranch" has to be a reference: sha1, origin/master, ...

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