Reputation: 492
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?
git cherrypick - This didn't work because of file 'X' in branch 'B' has hundreds of commits associated with it. It's pretty hard to cherrypick and also sometimes it might bring changes involved in other files.
git checkout C && git checkout 'branch B' -- path_of_X I created a branch 'C' from master branch 'A' and tried to checkout file 'X' from branch 'B'. Now All my changes of file 'X' from 'A' has vanished. Also, the git history for the file 'X' is lost.
git checkout C && git merge B --no-commit --no-ff I created a branch 'C' from master branch 'A' and merged branch 'B' completely with --no-commit and -no-ff options. This has brought all the changes from 'B'. Now I tried to discard all changes in 'C' from 'B' except the changes of file 'X'. Looks like there is no way to do a git discard excluding a file/folder.
What is the way to solve this problem? Any help would be much appreciated. Thanks in advance.
Upvotes: 1
Views: 570
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
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