Reputation: 13395
Let's say I've branches A
and B
.
A: a b c d
B: a b c e
I want to get commit e
from branch B
and put it under commit d
from branch A
. So I in the end result should look like this
A: a b c e d
How to do it?
Should I cherry-pick
commit e
first, put it on the top of d
and then switch places of d
and e
commits or is there another way? Also It possibly will have some conflicts - how to resolve them without creating additional commit?
Upvotes: 4
Views: 1621
Reputation: 209
Checkout to branch A and do the follwoing:
git reset --hard HEAD~1
git cherry-pick e
git cherry-pick d
And if you face conflicts while cherry-pick, follow this How do I resolve cherry-pick conflicts using their changes?
Upvotes: 0
Reputation: 1769
WARNING : rewriting history shouldn't be done if branches being rewritten have already been pushed In other terms, if commit d has already been pushed, you CAN'T do what you want without risking data loss, or generating confusing conflicts.
Okay, so let's assume A has not been pushed.
git rebase is the tool you want.
git checkout A
git rebase -i a
You enter interactive mode (in the configured editor for git), filled in with :
pick sha-of-b message-of-b
pick sha-of-c message-of-c
pick sha-of-d message-of-d
Just insert your cherry-pick at the right place with the x (exec) command :
pick sha-of-b message-of-b
pick sha-of-c message-of-c
exec git cherry-pick sha-of-e # <--- here
pick sha-of-d message-of-d
Git will :
If there any conflicts, it's time to resolve them normally, then do
git rebase --continue
to integrate commit d
If youd had conflicts cherry-picking e, you'll probably have conflicts again at this time. Resolve them, and run again
git rebase --continue
to finish rebase.
You can then do a git diff HEAD@{1} to check the difference.
Upvotes: 0
Reputation: 1723
You can cherry pick commit d
on top of branch B
. If you don't want to modify branch B
, create copy of branch B
as branch C
and make proper changes there.
Shortly:
git checkout -b C B // copy branch B to new branch C
git cherry-pick d // we are on C branch, cherry-pick d commit
//resolve conflicts as git is describing
You could also cherry-pick commit e
onto branch A
, but you might forced to resolve two conflicts instead of one:
git checkout A
git cherry-pick e //we are on A branch, cherry-pick e
//some conflicts may happen
git rebase -i c // rebase on c commit, this is the last one with proper order
//this will require to edit rebase picks - you can order them as you want
//after leaving editor, you might need to resolve conflicts again
If any conflict will occur, git will allow you to resolve it and changes will be saved in commit d
(it won't really be the same commit as it will have new parent and it will contain conflict resolving changes). git in command mode will give you all necessary hints about resolving conflicts when cherry-picking.
If you really need changes on branch A
(this shouldn't be necessary), do as I wrote above and hard reset branch A
to branch C
.
//Do as in first case
git checkout A
git reset --hard d2 //d2 is d commit on C branch
Upvotes: 3