How do I backport a commit in git?

Solution 1:

This is exactly the use case for git-cherry-pick

git checkout maintenance
git cherry-pick <commit from master>

Solution 2:

Alternate solution to using "git cherry-pick" (as recommended in other responses) would be to create a separate [topic] branch for the fix off maintenance branch, and merge this branch first into maintenance branch, then into master branch (trunk).

This workflow is (somewhat) described in Resolving conflicts/dependencies between topic branches early blog post by Junio C Hamano, git maintainer.

Cherry-picking results in duplicated commit, which down the line may cause problems when merging or rebasing. Topic-branch based workflow keeps only one copy of the fix.

Solution 3:

For complex commits that cannot be applied using git cherry-pick you can try

git checkout -b merge-branch master
git rebase --onto=`git merge-base master maintenance` HEAD~1 && git rebase master

Explained: http://blog.boombatower.com/automatically-backport-commits-using-git.