Posted By: David Joyner
I’m using Git on a new project that has two parallel — but currently experimental — development branches:
master: import of existing codebase plus a few modifications that I’m generally sure of
exp1: experimental branch #1
exp2: experimental branch #2
exp2 represent two very different architectural approaches. Until I get further along I have no way of knowing which one (if either) will work. As I make progress in one branch I sometimes have edits that would be useful in the other branch and would like to merge just those.
What is the best way to merge selective changes from one development branch to another while leaving behind everything else?
Approaches I’ve considered:
git merge --no-commitfollowed by manual unstaging of a large number of edits that I don’t want to make common between the branches.
Manual copying of common files into a temporary directory followed by
git checkoutto move to the other branch and then more manual copying out of the temporary directory into the working tree.
A variation on the above. Abandon the
expbranches for now and use two additional local repositories for experimentation. This makes the manual copying of files much more straightforward.
All three of these approaches seem tedious and error-prone. I’m hoping there is a better approach; something akin to a filter path parameter that would make
git-merge more selective.
You use the cherry-pick command to get individual commits from one branch.
If the change(s) you want are not in individual commits, then use the method shown here to split the commit into individual commits. Roughly speaking, you use
git rebase -i to get the original commit to edit, then
git reset HEAD^ to selectively revert changes, then
git commit to commit that bit as a new commit in the history.
There is another nice method here in Red Hat Magazine, where they use
git add --patch or possibly
git add --interactive which allows you to add just parts of a hunk, if you want to split different changes to an individual file (search in that page for “split”).
Having split the changes, you can now cherry-pick just the ones you want.