git version 1.7.5.4
Ubuntu 11.10
Is it possible to merge some selected pieces of source code across a few files?
I have been working on 2 different branches i.e. branch1 and branch2.
Currently I am on branch2, and there are some changes from branch1 that I want to add to branch2.
All the selected pieces I want are on a single commit on branch1.
I could just switch to branch1 copy the changes, then switch back to branch2 and paste them in. However, I would rather learn how git could do this for me.
Also, I am worried that if I use git I could merge some changes from branch1 that I do not want in branch2. So that is why I want to do it by selecting the pieces.
Many thanks for any suggestions,
That’s not really how git works. When you merge commits, you either merge the entire commit or you don’t merge any of it – otherwise, the integrity of the history graph would be compromised.
What you could do is:
git checkout -b temp_branch branch2git checkout branch1 /path/to/file/with_changesgit reset HEADgit add -pand only stage the portions of the file changes that you want.git commitgit checkout -f branch2git merge temp_branchThis would merge in a new, different commit from those on branch1 that had the partial changes – but note that if you later went to merge branch1 into branch2, you’d have to manually resolve the conflicts, because creating the new partial-changes commit makes a separate, distinct branch of history.