How do I prevent an automerge using Git?

Git

Git Problem Overview


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.

Update:

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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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.

https://stackoverflow.com/questions/5074452/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>

Solution 4 - Git

For vim & vim-fugitive users:

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

Solution 5 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmarckassayView Question on Stackoverflow
Solution 1 - GitFractalSpaceView Answer on Stackoverflow
Solution 2 - GiteckesView Answer on Stackoverflow
Solution 3 - Gitandrew pateView Answer on Stackoverflow
Solution 4 - Gituser3751385View Answer on Stackoverflow
Solution 5 - GitSimon RichterView Answer on Stackoverflow