9

I've read many posts related to this issue today( one useful one here: Fix a Git detached head? & Why did my Git repo enter a detached HEAD state?).

Today I've been scratching my head. I'm not able to understand how to fix this even after reading those articles.

Yesterday was all ok, I remember having given a checkout command. In the evening I found that Git was not showing any changes even though local changes were present. After researching today I could see now that git status is showing "HEAD detached from 99f040f".

Since then I removed .git folder in my D: (on my Windows 7) and restored .git of 27th July backup. Still not resolved. I tried 25th July version too. But no luck. So I've now restored yesterday's .git version.

I'm pasting the screenshot from gitk. Is there anything I can do to correct this issue? enter image description here

2
  • 1
    git checkout -b temp to backup what you have right now, git branch to see what branches are in your repo, and then git checkout <branchname> to get to your desired branch.
    – Ajedi32
    Commented Aug 1, 2013 at 13:40
  • possible duplicate of Git: How can I reconcile detached HEAD with master/origin?
    – kan
    Commented Aug 1, 2013 at 13:47

1 Answer 1

13

To get back to the last checked-out branch, simply type

git checkout -

Seems a bit under-documented (search for You may also specify in the docs of git checkout), but works for me.
Could also be used as a handy shortcut for switching between two branches:

git checkout master
git checkout branchwithaverylongnamethatyoudontwanttotypeagain
git checkout - # brings back master
git checkout - # brings back branchwithaverylongnamethatyoudontwanttotypeagain
4
  • In the <branch> option description: You may also specify - which is synonymous with "@{-1}".
    – rodrigo
    Commented Aug 1, 2013 at 14:16
  • Note that for branches with very long names that you don't want to type again, git supports tab completion of branch names.
    – Ajedi32
    Commented Aug 1, 2013 at 15:22
  • branchwithaverylongnamethatyoudontwanttotypeagain1 vs. branchwithaverylongnamethatyoudontwanttotypeagain2?
    – eckes
    Commented Aug 1, 2013 at 21:32
  • branchwitha<tab> + 1 or 2! :) Commented Jan 27, 2014 at 20:07

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