Fixing a merge commit in git
I messed up. I merged other branch to my branch. Changes on both sides were substantial so I spent extremely long time resolving conflicts. After committing the merge, I realized I had forgotten to add one file, so the merge was committed incomplete. I didn’t want to pollute the history with a “forgotten changes” commit so I decided to fix it before pushing. I called
git reset --soft HEAD~
Then I added the missed change and wanted to re-commit. However, git no longer knows it should be a merge commit. Is there any way out without re-doing the entire merge?
2 Solutions collect form web for “Fixing a merge commit in git”
What I would do, if I wanted to amend my last commit (whether it is a merge commit or a normal commit):
- Do the change (in your case add the file)
- Stage the change. E.g. with
git add .
Add the change to last commit with
git commit –amend
And you’re done
Since you aren’t at that point anymore, first you have to go back there. How?
List the last references you had in order to find out what was the hash of the merge commit you want to restore. Execute
Search in that list and copy the hash of the merge commit. Look for something like […] merge origin/blablabal, just below something like […]reset moving to HEAD~
Reset to that commit (as if you haven’t done the
git reset --soft HEAD~)
git reset –hard HASH_FROM_BULLET_2
Now you are at the point you just commited the original merge and now you can proceed with 4, 5 and 6 from the beginning.
you can checkout to a specific commit on a branch, therefore go back before the merge occured and redo it all.
Go on merged branch :
will show all commits on branch with associated ids (or key) just copy the first letters of the desired commit key
git checkout [commit_id you copied]
You should now be on the branch before the merge.
If you wish to continue browsing commits you can keep checkout from commit ids.