Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I backport a commit in git?

Tags:

git

So, I have a maintenance branch and a master branch in my project. If I make a commit in the maintenance branch and want to merge it forward to the master branch, that's easy:

git checkout master; git merge maintenance

But if I want to go the other way around, i.e. apply a commit made to master back to my maintenance branch, how do I do that? Is this considered cherry-picking? Will it cause problems or conflicts if I merge the maintenance branch forward again?

like image 543
Christian Oudard Avatar asked Sep 17 '09 17:09

Christian Oudard


3 Answers

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

git checkout maintenance
git cherry-pick <commit from master>
like image 171
mwalling Avatar answered Oct 11 '22 16:10

mwalling


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.

like image 38
Jakub Narębski Avatar answered Oct 11 '22 16:10

Jakub Narębski


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.

like image 39
boombatower Avatar answered Oct 11 '22 16:10

boombatower