athavan kanapuli
athavan kanapuli

Reputation: 492

Git Selective merge

I have a master branch 'A' and a feature branch 'B'.

Now a file called 'X' has evolved with some changes in both 'A' and 'B' branches.

How do I create a third branch 'C' ( From Master 'A') where I can get the file 'X', having both the changes from branch 'A' and branch 'B'. And also I don't need other commit changes from 'B'. I need only the change of file 'X' from 'B'.

What I tried?

What is the way to solve this problem? Any help would be much appreciated. Thanks in advance.

Upvotes: 1

Views: 570

Answers (2)

Yilun Chen
Yilun Chen

Reputation: 1

Let me provide my way to do git selective merge.

git checkout -p <branch> <path>

A most simple way is to call

git checkout -p <branch> ./

Upvotes: -1

ElpieKay
ElpieKay

Reputation: 30966

Find the merge-base of C and B.

base=$(git merge-base C B)

Create a temporary branch from $base.

git checkout -b temp $base

Squash merge B to temp.

git merge B --squash
git commit

Cherry-pick the squashed commit to C with -n.

git checkout C
git cherry-pick temp -n
# commit X only
git commit -m 'blah blah' -- path_to_X
# discard the rest introduced changes
git reset --hard
# if you want to add more changes, modify other files and use "git commit --amend" later

Remove the temporary branch.

git branch -D temp

Upvotes: 2

Related Questions