Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git pick commit from another branch and put it under current commit

Tags:

git

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?

like image 641
lapots Avatar asked Sep 14 '25 01:09

lapots


1 Answers

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
like image 150
zoska Avatar answered Sep 17 '25 06:09

zoska