1

I have a detached head in git. I am not interested to make any commit or save any changes, I just want to go to the head position of my branch. I have tried his command but it doesn't work:

 git checkout MyBranch

Any help?

---- Update

when I trying to checkout, I am getting this message:

 $ git checkout -f master
 Switched to branch 'master'
 Your branch is behind 'origin/master' by 6 commits, and can be fast-forwarded.

--- update 2 It says thetre are some changes, How can I remove them?

$ git merge origin/master
Updating fb5972a..28c2849
error: The following untracked working tree files would be overwritten by merge:
  ---
Please move or remove them before you can merge.
Aborting

How can I discard them?

These commands did not work:

$ git checkout -- .


$ git stash save --keep-index
No local changes to save
2
  • 2
    When you say "git checkout MyBranch doesn't work," what do you mean? Does it fail, and if so, how?
    – Matt Ball
    Commented May 8, 2012 at 10:56
  • See also Fix a Git detached head?.
    – user456814
    Commented May 30, 2014 at 5:16

3 Answers 3

5

this probably happens because you've made changes to the detached head.
try forcing the checkout, which will discard the changes, as you mentioned you don't care about those.

git checkout -f MyBranch

from the git help checkout man page:

  -f, --force
      When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes.

      When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored.

Switched to branch 'master'

So, you are on master branch now.

Your branch is behind 'origin/master' by 6 commits,

This means that the remote master branch, has updates, and that you may want to update your local branch to match the remote. You have already fetched the origin/master branch, thus git knows that your local branch is behind the remote branch.

and can be fast-forwarded.

This means that the remote branch has linear changes made to it, and that there will be no conflicts in merging/updating your local branch.

To update the local branch, just merge the origin/master branch to it.

git merge origin/master

error: The following untracked working tree files would be overwritten by merge:

this means that you have some files locally, that are not tracked (have never been git add'ed and commited) that the new changes (the merge) is going to add to the git list of files to watch and track. So the new files, that are going to be added, clutter with the local untracked files on the repo.

To resolve that, either remove those local files, or rename them, or move them to another location and try to merge the remote branch again.

0
0

You can stash your changes, which makes you working directory, relative to the detached head, clean, so you should then be able to checkout the branch you want.

Once on the desired branch you can stash pop to bring back those changes. There's lots of options in the manual ;-)

0

Do a git status to see if you changed any files. Then a git checkout . to remove any changed files, then you should be able to git checkout MyBranch to goo the head.

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