6

In case of Detached Head, how do I remove the commits ahead of Detached Head and turn Detached Head into master?

1
  • You can probably git checkout -b master to create a master branch at your current commit, which happens to be at a detached HEAD state Commented Apr 14, 2017 at 18:59

2 Answers 2

4

It's not quite clear what you mean.

If you want to "Not be in a detached head state anymore, but instead have master checked out, but have master be at the same commit I'm at now with my detached head.", you want to do this:

git tag before
git checkout master
git reset --hard before
#git tag -d before
1
  • I have this situation happen so often that I'm looking for an way that doesn't involve making a branch all the time. And this is it.
    – Mote Zart
    Commented Aug 23, 2022 at 15:45
1

Use the git reset command to make the current branch point to a commit.

Specifically, get the commit ID that you want to point to, git checkout master, then git reset <ID>.

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