How to prevent an automerge using git?
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.
When I use Git’s difftool command, I am able to diff and select what I want to be added into the master branch. But then when I do a merge, I will lose what I selected prior because Git will do an automerge. I can commit the changes into master prior to the merge, but doing so seems unnatural.
And Git’s mergetool is only available when there are conflicts from a merge. But if Git does an automerge then usually there aren’t conflicts, so I am unable to run the mergetool command.
I am starting to think what I am trying to accomplish is bad practice or it’s just not possible. That is, to merge a topic branch and only have it merge what I need from diffing. And in an addition, to have this reflected in history. At any rate, the question I posted surfaced when experimenting with git.
4 Solutions collect form web for “How to prevent an automerge using git?”
What you are trying to do is 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. Here is what you will need to do:
$ git checkout master $ git difftool -t kdiff3 local-branch HEAD
KDiff3 window, the left hand side (
A) is your local-branch and the right hand side (
B) is your current branch (master).
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 (
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.
git merge --no-commit --no-ff <local-branch>
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.
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>
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