7

I have several git branches that have commits cherry-picked between them. Commits are sometimes squashed.

In order to decide whether to apply a commit abcdef to the live branch, I want to find out if an equivalent commit has already been applied to the preprod branch.

If preprod never squashed commits, I would use patch IDs, as discussed in this question. Since preprod may squash sequences of commits, I want to see if abcdef would be an empty commit on preprod.

$ git checkout preprod
$ git cherry-pick abcdef
On branch preprod
Your branch is up-to-date with 'origin/preprod'.
You are currently cherry-picking commit abcdef.

The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

    git commit --allow-empty

Otherwise, please use 'git reset'

How can I programmatically detect if cherry-picking a commit would result in an empty commit?

(I know this is an imperfect heuristic for detecting whether the patch has been applied, but it's good enough for my use case.)

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
  • You can use `git diff` from a bash script and check the result, rather than doing a full cherry-pick. If git diff shows no differences, then you should be good to go. https://stackoverflow.com/a/13715727/211627. Unfortunately, squashing a prod branch changes the history, so you've wiped out the best way of determining whether or not a patch has been applied. I'd strongly advise against doing that in the future. Kind of invalidates the whole point of a code versioning tool. – JDB Dec 08 '17 at 17:37

1 Answers1

4

Take a look at git cherry:

Find commits yet to be applied to upstream

Something like this should do the trick by displaying commit abcdef if a cherry-pick is necessary else nothing (didn't check):

git cherry abcdef preprod abcdef^
zigarn
  • 10,892
  • 2
  • 31
  • 45