Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy commits from one branch to another

Tags:

git

I have two features I'm working on

Shared -- Commits -- A -- B -- C Feat1

Shared -- Commits -- D -- E -- F Feat2

The problem is that to test Feat2 I really need Feat1 as well, but I still want them as separate branches because they are distinct. What is a good way to make changes to Feat2 and then quickly make a third branch that is both of them together that doesn't require me to destroy it every time.

Shared -- Commits -- Feat1 -- Feat2

What I was doing is

git checkout feat2 git branch -b combo git rebase -i feat1

But then when I make updates to feat2 I don't know how to merge in those new changes.

like image 341
Nate Avatar asked Oct 08 '15 19:10

Nate


People also ask

Can I copy a commit to another branch?

You can cherry-pick a commit on one branch to create a copy of the commit with the same changes on another branch. If you commit changes to the wrong branch or want to make the same changes to another branch, you can cherry-pick the commit to apply the changes to another branch.

How do you pick a commit from one branch to another?

Cherry picking is the act of picking a commit from a branch and applying it to another. git cherry-pick can be useful for undoing changes. For example, say a commit is accidently made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong.


2 Answers

There are basically two options here.

  1. You can use cherry-pick to copy the new commits on feat2 to combo.
  2. Instead of rebasing, you can merge

Cherry Picking

To cherry pick new commits on feat2 to combo, run the following:

git checkout combo
git cherry-pick <new commit> <new commit> <new commit>

Merging

Or alternately, you can merge instead of rebasing. E.g. To create combo:

git checkout -b combo feat1
git merge feat2

Then, to "merge in new changes" from feat2, just...

git merge feat2

Likewise, to merge in new commits on feat1:

git merge feat1
like image 134
Ajedi32 Avatar answered Sep 18 '22 18:09

Ajedi32


Merging

You can merge instead of rebase. Since your combo branch is just for testing, you don't have to worry about the history being a bit messier.

That is:

git checkout feat2
git branch -b combo
git merge feat1

Later, when you update feat2:

git checkout combo
git merge feat2

Whenever you make an update to feat1 or feat2, merge that into combo.

The disadvantage is that you'll have to merge all commits in both branches. If there are changes you don't want, you'll have to make a separate commit removing those changes in the combo branch.

Rebasing

You can rebase the additional commits from feat2 onto combo.

Suppose you add some commits onto feat2, then to move these commits to combo:

git rebase --onto combo lastcommittoexcludeonfeat2 feat2

Alternatively, before you make the changes to feat2, add a branch or tag, eg:

git checkout -b lastfeat2rebase
git checkout feat2
git commit ... # more commits here to feat2
git rebase --onto combo lastfeat2rebase feat2

Please note, that once you go with the rebase option, you cannot use the merge option anymore. If you use the merge option, you can later decide to go with the rebase option.

like image 28
ronalchn Avatar answered Sep 22 '22 18:09

ronalchn