How to merge all files manually in Git?

70,859

Solution 1

There is much simpler way:

git merge --no-commit merge_branch

As man says:

With --no-commit perform the merge but pretend the merge failed and do not autocommit, to give the user a chance to inspect and further tweak the merge result before committing.

Solution 2

I had a scenario where:

git merge --no-commit merge_branch 

just caused a Fast Forward.

If this happens you can use:

git merge --no-commit --no-ff merge_branch

and then you'll be able to review your changes

Solution 3

A similar question is How to prevent an automerge using git?

FractalSpace gave an answer which I think useful:

$ git checkout master
$ git difftool -t kdiff3 local-branch HEAD

The idea is using difftools instead of auto-merging tools to manually pick what you need and create new files.

Solution 4

For anyone who just needs to micromanage a merge, jump to the Hybrid section below.

For anyone who is wondering about the difference between @True's answer using git difftool and the other answers that use git merge, see Git mergetool vs difftool.

Here are more details:

Difftool

If you have git configured to use a modern diff.tool such as kdiff3, meld, or vimdiff, you'll be able to manually merge using that diff tool, and the command line can be simple:

git difftool other_branch

...this will let you do a two-way manual merge between your current branch and other_branch (described as $LOCAL and $REMOTE in man git-config).

Mergetool

The "correct" way the other answers discuss would be to instead configure git to use e.g. kdiff3 or vimdiff as your merge.tool, and use:

git merge --no-commit --no-ff other_branch
git mergetool

...this command can do an N-way manual merge between $BASE, $LOCAL, and $REMOTE, into $MERGED. See https://stackoverflow.com/a/2235841/1264797 for one example of how to configure git. You many not need to configure the mergetool.*.cmd entry at all if you use one of the tools git already knows about. (Meld can only show three panes, so if you use meld with the default settings, you'll not see $BASE.)

Difftool vs mergetool

Someone might jump in to correct me, but the main differences between the above difftool and mergetool techniques seem to be:

  • mergetool can do an N-way merge if configured accordingly
  • mergetool adds other_branch as a parent on the new commit so history works correctly
  • difftool lets you manually see and select each and every changed line, but you lose the above two benefits of mergetool

Hybrid -- best of both worlds

A method that combines merging and diffing would look something like this:

git merge --no-commit --no-ff other_branch
git mergetool
git difftool HEAD
git commit

...this does the N-way merge to resolve conflicts and make history work right, and then shows you the complete set of diffs so you can review and tweak before you commit.

Solution 5

I found the other answers unsatisfactory and became frustrated searching for an answer. A solution to this question I finally found here: https://stackoverflow.com/a/11593308/1351182

If you run these commands, you will create a new commit which essentially takes the latest commit of branchToMergeFrom and allows you to apply a patch on top of it, which I think is like an additional commit on top.

git checkout branchToMergeTo
git checkout --patch branchToMergeFrom [file]

You will then be prompted (file-by-file if you didn't specify file) on exactly which 'hunks' you want to merge. In this way it walks you through each part of what would have been the automatic merge process and instead asks for manual arbitration on which bits and pieces you want to accept from the mergefrom branch. Here's an example of what it looked like in my project:

@@ -249,7 +251,8 @@ def draw_everything():
  
     draw_bg()
     draw_balls(ax)
-    plt.show(block=False)
+    if show:
+        plt.show(block=False)
 
 def advance(ms, accel_fun, collision_matrix_fun):
     global balls
(3/6) Apply this hunk to index and worktree [y,n,q,a,d,K,j,J,g,/,e,?]?

After typing y and <Enter>, I was presented with the next hunk, (4/6) for that file. This prompt at the bottom lets you simply accept the merge 'hunk' with y, reject it with n, or even go in and edit it manually. Here are the options:

y - apply this hunk to index and worktree
n - do not apply this hunk to index and worktree
q - quit; do not apply this hunk or any of the remaining ones
a - apply this hunk and all later hunks in the file
d - do not apply this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help

I wanted to go in and manually edit one hunk, as I didn't want to accept or reject the merge exactly as it was posed. So I chose e and was given a file to edit. I was pleased when I noticed there were even instructions at the bottom on how to edit the hunk properly. You can even split hunks into smaller ones with the s option as above.

I would recommend this process if what you want is manual merging where you still leverage the automatic process as much as possible. The difference is that you get to oversee every merge 'hunk' and edit them as you please. I hope this helps future readers.

After this process, you probably want to run git checkout branchToMergeTo && git merge branchToMergeFrom in order to formally merge the history of branchToMergeFrom into branchToMergeTo.

Share:
70,859
gennad
Author by

gennad

Friendly :)

Updated on March 15, 2021

Comments

  • gennad
    gennad about 3 years

    I want to merge all files manually with meld or any other diff tool, how can I do this with Git?
    When I run git mergetool it says no files need merging. So I suppose I can do it only if I have conflicts.

  • James
    James over 9 years
    Thank you. Is there perhaps a way to use mergetool/meld to review and merge the changes piecewise with a nice threeway interface?
  • Maxim
    Maxim about 8 years
    Is it possible to pull it in the same way? I need to verify ALL conflicts manually if fast-forward is not possible.
  • Løiten
    Løiten over 7 years
    By adding --no-ff the fast forwarding is disabled, which will happen if there are no conflicts.
  • Top-Master
    Top-Master over 5 years
    @Maxim no but since pull is just the combination of fetch and merge do first call git fetch merge_branch then latter do merge
  • Top-Master
    Top-Master over 5 years
    using both --no-commit --no-ff is like having .gitattributes file in root dir containing * -merge, but with per merge choice to use it or not
  • Pluto
    Pluto over 5 years
    And of course, use git mergetool if there are conflicts.
  • stevegt
    stevegt about 5 years
    I've added an answer comparing this answer to the git merge answers to help clarify things for anyone else who stumbles into this page.
  • ThomasMcLeod
    ThomasMcLeod almost 5 years
    This is still an automatic merge, so this doesn't answer the origin question.
  • Unknow0059
    Unknow0059 over 3 years
    I get Automatic merge went well; stopped before committing as requested upon running that. When I try to run git mergetool it says No files need merging.
  • porton
    porton over 3 years
    When I try to manually merge this way with kdiff3 or meld, it saves changes to a temporary file in /tmp and does not change the working dir. But I need to store changes to the working dir.
  • porton
    porton over 3 years
    When I try to manually merge this way with kdiff3 or meld, it saves changes to a temporary file in /tmp and does not change the working dir. But I need to store changes to the working dir.
  • Cutton Eye
    Cutton Eye over 3 years
    the history might get lost here. Also this dies not show up like a merged branch. For git this might just look like a huge commit out of the void.
  • stevegt
    stevegt about 3 years
    @porton You may have to do some more experimentation, but one of the panes should be the working dir file, or should be a file that gets automatically copied into the working dir after you exit. I don't remember which, and it may be dependent on your git version and tool config.
  • grenix
    grenix over 2 years
    Just speculating this method is effectively like copying files from another source (eg a clone from this repository with current branch 'merge_branch' and clean). In this case I would expect only 'git difftool' showing any changes.