2

So I have 3 branches;

  • develop - my continued development branch
  • version_1 - a release branch
  • version_2 - a release branch

I had to do a hotfix on version_2 to reship that version, it was a 2 line change in 2 files, very small.

I wanted to apply that fix to version_1 and develop.

So I;

git checkout version_1
git merge <commit checksum>

I thought a commit only contains the changes, so would only apply those. But the merge conflicts because it tries to update all the changes between the two branches.

Is there a way to merge/move/apply ONLY the changes in a small commit to other branches?

I could just manually re-implement the hotfix on those branches, but this seems a lot of hard work, especially as more hotfixes may need to re applied and rolled out to all other branches.

Cheers, Will
Will Hancock
  • 1,240
  • 4
  • 18
  • 27

2 Answers2

3

Merging only one or a few commits mean using git cherry-pick

First cancel the merge you just did: see "Undo a Git merge?".
Then:

git checkout version_1
git cherry-pick <commit checksum>

That can apply to multiple commits or a range of commits: see "How to cherry pick a range of commits and merge into another branch".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Note to self: And that was my **10000th answer** on Stack Overflow (in 68 months), less than 6 months after the [9000th answer](http://stackoverflow.com/a/20683667/6309). Before that [8000th answer](http://stackoverflow.com/a/17569094/6309), [7000th answer](http://stackoverflow.com/a/14274272/6309), [6000th answer](http://stackoverflow.com/a/11644343/6309), [5000th answer](http://stackoverflow.com/a/7917396/6309), [4000th answer](http://stackoverflow.com/a/4953561/6309), [3000th answer](http://stackoverflow.com/a/3074849/6309), [2000th answer](http://stackoverflow.com/a/2027976/6309). – VonC May 28 '14 at 13:02
2

You need to cherry-pick those commits.

Switch to the branch where you want to add the commits:

git checkout develop

Then, cherry-pick the commit. First do a git reflog and get the SHA-1 of the commit of the hotfix.

Then, while you are on the branch develop, cherry-pick it

git cherry-pick <commit SHA obtained above>

Perform similar actions on the other branch version_1.

gravetii
  • 9,273
  • 9
  • 56
  • 75