In case of Detached Head, how do I remove the commits ahead of Detached Head and turn Detached Head into master?
2 Answers
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
-
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. Commented Aug 23, 2022 at 15:45
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>
.
git checkout -b master
to create amaster
branch at your current commit, which happens to be at a detached HEAD state