git cherry-pick is yet another great/unknown feature of git. We use it in libguestfs to cherry pick the best bug fixes from the development branch into the stable branch. The whole process is quite effortless.
First I will list out all the main branch [ie. devel/unstable] commits which have happened since the last stable release, and I’ll look at each commit. The only difficulty is evaluating each commit to see whether it meets our criteria for a sufficiently safe change that users of our stable branch will want.
Secondly, I have our stable branch checked out in my working directory, and “git pull” to make sure that is up to date.
Then, for each commit I want to cherry pick, I simply do:
$ git cherry-pick -x sha1_of_the_commit
And usually that’s all I have to do. Git’s patch conflict resolution is much better than plain “patch”. It’s able to work minor miracles even where the code in the two branches has diverged quite a way. If it’s unable to apply the patch directly, then you’ll see a message like this:
Automatic cherry-pick failed. After resolving the conflicts, mark the corrected paths with 'git add <paths>' or 'git rm <paths>' and commit the result. When commiting, use the option '-c 94e310d' to retain authorship and message.
This is fairly self-explanatory. Use “git status” to see which files are problematic:
$ git status # On branch stable-1.2 # Your branch is ahead of 'origin/stable-1.2' by 15 commits. # # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: tools/virt-cat # modified: tools/virt-edit # modified: tools/virt-ls # modified: tools/virt-tar # modified: tools/virt-win-reg # # Unmerged paths: # (use "git reset HEAD <file>..." to unstage) # (use "git add <file>..." to mark resolution) # # both modified: perl/lib/Sys/Guestfs/Lib.pm
Edit the file to manually resolve the conflict, add the file, and then commit with the “-c” option noted in the original message.
The result is a series of stable commits like this, and hopefully some happy users.