I am trying to merge a local branch into the master branch without having Git to do an automerge. I would like to “hand pick” what I would like to be merged into master.
I can see you may wish to do this if you do not trust auto-merge, because two edits in different places in a file (which are done on different branches) may not cause auto-merge to raise a conflict but may not actually work together.
You may want to look at using a custom merge driver. This page describes how to go about it.
Git - how to force merge conflict and manual merge on selected file
An alternative would be to find the files that differ between the branches first, before performing the merge, then checkout the file into the appropriate branch to ensure you get a clean merge and functional code that contains either one or the other of the two edits.
git diff --name-only <branch1> <branch2>
git checkout <branch1> -- <paths>
git merge --no-commit --no-ff <local-branch>
does it.
When you executed it, the changes from local-branch
are applied but not yet staged.
Then, you could look at the changes to be applied and – in case that you want to take them all – apply them with
git commit -a
Otherwise, select the files to be taken, stage them with git add
and finally commit them with git commit
. Restore the unwanted files then with git checkout -- filename
.
You are trying to bypass Git from getting involved in the merge process and to hand-pick each line of each modified file to be merged. This not the same as git cherry-pick
. Neither will git merge --no-commit
, etc. help. You will need to do:
$ git checkout master
$ git difftool -t kdiff3 local-branch HEAD
In the KDiff3 window, the left hand side (A
) is your local-branch and the right hand side (B
) is your current branch (master).
Select Merge | Merge Current File
from the menu (or press the colorful diamond shaped icon with the same title).
You will then be shown a diff and conflicts (if any) for each file. And you will have the ability to pick left or right side (A
or B
), or both, and/or manually tweak the merged file.
On another note, something is telling me you have some bigger issues with your workflow.
Add the following to ~/.gitconfig
[difftool "fugitive"]
cmd = vimdiff $LOCAL $MERGED $REMOTE
trustExitCode = false
keepBackup = false
Now use Fugitive for a 3 way diff
$ git checkout master
$ git difftool -t fugitive somebranch HEAD
Note on $LOCAL, $MERGED, $REMOTE:
$LOCAL The file on the branch where you are merging; untouched by the merge process when shown to you
$MERGED The partially merged file, with conflicts; this is the only file touched by the merge process and, actually, never shown to you in meld
$REMOTE The file on the branch from where you are merging; untouched by the merge process when shown to you
I think you want to cherry-pick individual changes:
git checkout master
git log --reverse --oneline topic ^master
and then invoke git cherry-pick
for each commit you want to have in the master branch.
If the remaining changes in the topic branch should be preserved, you may also want to rebase this branch on top of the new master:
git rebase master topic