How to get out of Git Detached Head Merge Hell

Abu Eesa
FusionQA
Published in
Jun 6, 2019

Recently I was in a position where I wanted to keep my local changes and also keep the changes from the master branch.

But somehow I had ended up in a detached head and was worried I’d lose my work.

To fix this situation (if you want to keep your local changes on the detached head and merge them to master) follow these steps:

Step 1:

git log -n 1

This will tell you the last commit on the detached head. Copy the hash for the latest commit (we’ll call this copied_commit_hash).

Step 2:

git checkout master (or whatever branch you want to merge into)

Step 3:

git branch temp <copied_commit_hash>

Your local changes will not be saved in a new branch named ‘temp’.

Step 4:

If you would like to add your local changes to the master branch, from the master branch execute this command:

git merge temp

Step 5 (final step):

It is likely you will have some merge conflicts, fix the merge conflicts one at a time and then git add, git commit and git push to master.

--

--