2

When I'm doing a git cherry-pick and there's a conflict, git stops short of making the commit and allows me to resolve the conflict.
Is there a way to force this stop even if there are no conflicts?
The reason I want to do that is that I have a change I want to cherry-pick but I don't want all of the files it changes. Having it stop just before making the commit allows me to remove the file from the commit and then do git cherry-pick --continue without it

git cherry-pick --no-commit does something similar to what I want but not exactly.
If you do git cherry-pick --no-commit and then git commit the result is different than what you get if you do git cherry-pick, resolve a conflict and then do git cherry-pick --continue

shoosh
  • 76,898
  • 55
  • 205
  • 325

2 Answers2

3

Maybe you are searching for git cherry-pick --no-commit

-n --no-commit

Usually the command automatically creates a sequence of commits. This flag applies the changes necessary to cherry-pick each named commit to your working tree and the index, without making any commit. In addition, when this option is used, your index does not have to match the HEAD commit. The cherry-pick is done against the beginning state of your index.

This is useful when cherry-picking more than one commits' effect to your index in a row.

From here.

YesThatIsMyName
  • 1,585
  • 3
  • 23
  • 30
  • 1
    --no-commit doesn't do exactly what I want. It just adds the changes to the files but it doesn't take the metadata from the original commit. If I were to do `git commit` after a cherry-pick with `--no-commit`, the author of the commit would be me. That's different from what happens on a normal cherry-pick – shoosh Sep 12 '19 at 09:26
  • 1
    @shoosh You can do `git commit -m "Hello Universe" --author="Philip J Fry "` – YesThatIsMyName Sep 12 '19 at 09:28
  • I guess... It's strange that this is a state that can't be forced without an actual conflict – shoosh Sep 12 '19 at 09:30
  • As hinted before, the normal `cherry-pick` can apply multiple commits. I do not know exactly what happens if you run into a conflict, but your working tree can only hold one commit at a time - and logically, when you're editing to fix the conflict, _you have become the author_. I must admit I haven't paid attention to what actually happens with `cherry-pick`, but I would be very surprised if the conflict case is not equivalent to `git cherry-pick --no-commit` followed by `git commit`. On the other hand, when there is no conflict, git can just attribute the original commits' metadata correctly. – Amadan Sep 12 '19 at 10:51
  • So if I'm right, you can `git cherry-pick --no-commit` to see if there were conflicts, if there weren't then reset the working tree and use `git cherry-pick` to trigger the "I didn't touch anything" cherry picking, and if there were, then proceed with the "I have to clean up this mess" workflow (fix stuff, commit, make yourself the author). – Amadan Sep 12 '19 at 10:55
0

This is not a direct answer, but you could just try doing a soft reset after a successful cherry-pick with no merge conflict:

git reset --soft HEAD~1

This would place the change set from the cherry-pick merge into the stage, and also sync up your working directory with those same changes. From there, you could poke around and make whatever other changes you need, and then re-commit.

I am assuming here that you are cherry-picking a single commit.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360