Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect if a git patch is already applied?

Tags:

git

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.)

like image 936
Wilfred Hughes Avatar asked Dec 08 '17 17:12

Wilfred Hughes


People also ask

How do I find git patch files?

In order to create Git patch file for a specific commit, use the “git format-patch” command with the “-1” option and the commit SHA. In order to get the commit SHA, you have to use the “git log” command and look for the corresponding commit SHA.

How do I undo a git patch?

Sometimes people run diff with the new file first instead of second. This creates a diff that is “reversed”. To apply such patches, give patch the --reverse ( -R ) option. patch then attempts to swap each hunk around before applying it.

How do git patch files work?

A patch is a text file whose contents are similar to Git diff but along with code it contains metadata about commits, for example, a patch file will include commit ID, date, commit message, etc. We can create a patch from commits and other people can apply them to their repository.


1 Answers

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^
like image 68
zigarn Avatar answered Sep 29 '22 16:09

zigarn