14

I cherry-picked a commit and resolved the conflicts, added them, now when I try to do git amend it fails with below message.

fatal: You are in the middle of a cherry-pick -- cannot amend.

Why does git gives this message, is there a way to tell it things are fine?

garg10may
  • 5,794
  • 11
  • 50
  • 91

4 Answers4

14

I manually removed .git/CHERRY_PICK_HEAD now git doesn't know I did a cherry-pick, so amend works like it was a normal commit amend.

cherry-pick --continue adds a new commit message and would need to rebase which is a hassle.

garg10may
  • 5,794
  • 11
  • 50
  • 91
  • 1
    This works, but it would be better not touch files in .git directory manually. you can actually first cherry-pick --continue to add new commit, then revert the commit by "git reset HEAD~", at this point you are not longer in process of cherry-pick. so you can commit amend. – Sola Yang Sep 11 '19 at 18:28
8

It seems that you did a cherry-pick before, which failed due to conflicts. Thus, git thinks you're still in the middle of the cherry pick, since it expects you to fix conflicts, add conflicted files and run git cherry-pick --continue.

Your options here are to run git cherry-pick --abort which will abort the cherry pick, i.e. return the conflicted files to their previous state, possible losing changes, or to run git cherry-pick --continue, which will continue the cherry pick. When you do not remember when and what you did with the cherry-pick, this is probably the better option, althoough you should watch your repository closely before and after the --continue command.

Both commands will get you out of the cherry-pick state and allow you to perform the amend.

kowsky
  • 12,647
  • 2
  • 28
  • 41
1

The behaviour is defined in the documentation.

What you need in your case is

--continue
Continue the operation in progress using the information in .git/sequencer. Can be used to continue after resolving conflicts in a failed cherry-pick or revert.

Minzkraut
  • 2,149
  • 25
  • 31
0

When running actions like cherry-pick, rebase or merge, git keeps track of several info on the base commit.

It looks like a reasonable safety to prevent the base commit from being modified.


I would advise you to somehow finish your cherry-pick, and then use rebase -i to modifiy the sequence of commits you want.

If we use the following names :

--*--*-- ... --A--B <- HEAD
      \
       *-- ... X <- cherry-picked commit

# you are currently on B, you run :
git cherry-pick X

If you wish to reach :

--*--*-- ... --A--B'--X' <- cherry-picked version of X
      \           ^
       \          a modified version of B
        *-- ... X

you can :

# add the modifications intended for B' :
git add -p [files]
# create a regular commit, using "git commit" :
git commit

--*--*-- ... --A--B--C

# complete the cherry-pick process :
git add [files with solved conflicts]
git cherry-pick --continue

--*--*-- ... --A--B--C--X'

# use 'rebase -i A' to squash B and C into one commit
git rebase -i A
# set the rebase actions to "squash C into B"

# the result should be what you expect :
--*--*-- ... --A--B'--X'
LeGEC
  • 46,477
  • 5
  • 57
  • 104