Is there a way to undo detached HEAD state without undoing the work I've done while in detached HEAD state?

56 Views Asked by At

I'm in a detached HEAD state but have made a bunch of commits while in detached HEAD that I can't lose. Is there a way to undo the detached HEAD state without losing any of my work?

1

There are 1 best solutions below

0
On

Just make a branch. That will preserve all the work you've done.

# detached head
# work, add, commit; work, add, commit ...
git switch -c saveMe

If you want to move your preservation branch commits to a branch that already existed, where you should have been all that time, simply rebase your preservation branch, from the detached head commit to the end, onto the existing branch.

git rebase --onto existingBranch <detachPoint> saveMe

You can then move the existing branch pointer to the end of the preservation branch, and throw away the preservation branch.

git switch existingBranch
git reset --hard saveMe
git branch -D saveMe

Thus it will be as if you had done all that work while on the existing branch, as if you had never been in detached head mode at all.