Cherry-pick with dry run

May 24, 2012

There will be a case which you may want to test a change or a patch from a different repository before actually committing it into your branch. This is a common case where you are working in a project which has multiple branches for various purposes. You are probably end up working on a release branch, but you will occasionally check and pick up good fixes made in master branch which is used for active development. But your release branch was forked a while ago and could be very diverged from the now master branch, so your cherry-pick may work without a conflict, but it can caused issue at runtime, so it’s always a good practice to test it first!

Git’s cherry-pick command does not support a flag for dry-run, but you can still use dry-run with few extra steps.

You can first generate a patch file based on a given SHA1 which you are interested to cherry-pick.

$ git format-patch -1 <SHA1>

You have 2 ways to use this patch file:

$ git am -3 <patch_file>


$ git am –3way <folder_contains_many_patch_files>/*
$ patch -p1 < <patch_file>

(You may have to resolve conflicts here manually)

$ git commit -a

If you are interested in trying to apply the patch to see whether you will have conflicts, you can also the command with patch –dry-run -p1.

You may have conflicts, and you will have to resolve them manually. After that is completed, you can kick off a local build. By doing it this way, you always verify those fixes you just picked from master branch into your release branch, and have them tested locally. It’s always a good idea to test everything before pushing back to repository which can pollute the branch for others.

Tags for this post:
  • git
  • comments powered by Disqus