41

When trying to commit after a merge I'm getting this error message:

"fatal: You are in the middle of a merge -- cannot amend."

How do i resolve this? as far as I know I have resolved all conflicts, i just need to complete the merge and commit the changes. but the product won't let me and doesn't give me any clues as to what I am meant to do next, and there is no option to "complete the merge"

Everytime I try to commit my changes I get the error message and I now have no idea what to do about it!

johnvdenley
  • 739
  • 1
  • 7
  • 16

5 Answers5

66

You can manually delete .git/MERGE_HEAD and Git won't be able to tell that you were just doing a merge. It will let you amend the previous commit with the changes in your index just like normal.

Please read:

Though this would work, it is a hack and not recommended. All is needed here is to let git know the merge is completed git commit -a as per this answer

T04435
  • 12,507
  • 5
  • 54
  • 54
Rag
  • 6,405
  • 2
  • 31
  • 38
  • Hey Brian! Thanks for the tip. Worked for me during an interactive rebase where I wanted to amend a commit. Can you explain what this is doing and point me somewhere that you learned what this was? Cheers! – technoplato Jun 05 '16 at 19:52
  • 5
    @halfjew22 Git just uses that file to record the ongoing merge. If you remove it, git has no way to know that you previously started a merge. You can also use `git merge --abort` if you're using the command line. – Rag Jun 05 '16 at 22:20
  • Thanks a lot Brian. I haven't tried it in this particular case, but won't git merge --abort also get rid of the changes to my working tree? I'm trying to amend a merge, it that makes sense... – technoplato Jun 05 '16 at 23:20
  • 1
    is this really safe? – Kyle Baker Oct 12 '16 at 15:49
  • 1
    this seems a hack, there should be some way to tell git merge is complete. Git maybe raising it for a reason? – garg10may Mar 20 '18 at 07:49
26

Do a git commit -a once you have resolved the conflicts. This is the last step when you are merging conflicts.

user376507
  • 2,004
  • 1
  • 19
  • 31
1

After resolving the conflicts,you should try "git rebase --continue" for the rebase to complete.Post that, commit --amend is allowed.

0

This happens because you have files conflict. When you do a git merge branch and don't have any conflict, git makes a commit automatically, then you must do a git commit --amend to change the commit message. But, when there is conflicts, there is no commit, because git expect you to resolve them, so when you finish to solve the conflicts, just do a git commit without --amend.

0

If you are trying to amend on a previous commit and you know there will be merge conflicts then force push the changes (if you are sure that you want your current changes to override remote changes).

First commit:

 1. git add test.txt
 2. git commit -m "some changes"
 3. git push

Second commit after some changes in same file which will result in merge conflict and we know we want only the latest changes that we did in this file:

1. git add test.txt
2. git commit --amend
3. git push -f

Otherwise, we might get stuck in merge and commit loop.

quintin
  • 812
  • 1
  • 10
  • 35