74

I did cherry-pick from a gerrit review in my branch. In gerrit code review, I have two patch sets and I cherry-picked patch one before, so now I want to do the second patch set, but there are conflicts, how can I force git to accept all changes? Thanks!

Jiang
  • 812
  • 1
  • 7
  • 8

4 Answers4

139

You can tell it to always prefer the changes of the commit you are cherry-picking:

git cherry-pick commitish --strategy-option theirs

commitish can be a SHA-1 hash of a commit, or a branch-name for the lastest commit of that branch, branch-name~1 for the commit before that etc.

If you want to do the reverse, use:

git cherry-pick commitish --strategy-option ours

The shorthand for --strategy-option is -X (uppercased X).

PS: What is the commit-ish, tree-ish?

emrcftci
  • 2,954
  • 3
  • 17
  • 34
CodeManX
  • 10,265
  • 5
  • 46
  • 64
  • 1
    if you want to accept incoming changes then you should use `theirs` key otherwise should use `ours`. u saved my day! – emrcftci Apr 28 '21 at 11:05
17

git cherry-pick -X theirs <commit-hash-you-want-to-force-cherry-pick-from>

My usual workflow is as follows:

Assuming I'm on the master and I have just made a commit.

  1. I grab the commit hash of that commit.
  2. Then checkout on to the branch I want to have such commit.
  3. Then run the command above, e.g. git cherry-pick -X theirs 5cf3412
tallamjr
  • 1,082
  • 14
  • 19
7

If you are already in conflict state, simply do

# add only conflicting files here
git checkout --theirs path/to/file
git add path/to/file
git cherry-pick --continue
geek-merlin
  • 1,312
  • 15
  • 12
0

you could brute force it with something like this:

git show cb1e6a:path/to/filename > path/to/filename
git add path/to/filename
git commit

but I'm sure there's an easier way.

Jason B
  • 438
  • 4
  • 7