Daniel Daniel - 1 year ago 113
Git Question

Fix a Git detached head?

I was doing some work in my repository and noticed a file has local changes. I didn't want them anymore so I deleted the file, thinking I can just checkout a fresh copy. I wanted to do the git equivalent of

svn up .

git pull
didn't seem to work. Some random searching led me to a site where someone recommended doing

git checkout HEAD^ src/

is the directory containing the deleted file).

Now I find out I have a detached head. I have no idea what that is. How can I undo?

Answer Source

Detached head means you are no longer on a branch, you have checked out a single commit in the history (in this case the commit previous to HEAD, i.e. HEAD^).

You only need to checkout the branch you were on, e.g.

git checkout master

Next time you have changed a file and want to restore it to the state it is in the index, don't delete the file first, just do

git checkout -- path/to/foo

This will restore the file foo to the state it is in the index.