How to merge all files manually in Git?
I want to merge all files manually with meld or any other开发者_开发知识库 diff tool, how can I do this with Git?
When I rungit mergetool
it says no files need merging
. So I suppose I can do it only if I have conflicts.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.
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
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.
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 accordinglymergetool
adds other_branch as a parent on the new commit so history works correctlydifftool
lets you manually see and select each and every changed line, but you lose the above two benefits ofmergetool
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.
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
.
Note, if you insists of merging manually (perhaps for a certain class of files), you still can define a merge driver.
You have a concrete example in "Git - how to force merge conflict and manual merge on selected file".
That way, your merge driver script can call any merge tool you want.
I pick strategy ours (it exist also as a pick in TortoiseGit), after doing a manual diff where you have brought-in changes you wanted manually.
From: https://git-scm.com/docs/merge-strategies
The merge mechanism (git merge and git pull commands) allows the backend 'merge strategies' to be chosen with -s option. Some strategies can also take their own options, which can be passed by giving -X arguments to git merge and/or git pull.
ours
This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches. Note that this is different from the -Xours option to the 'recursive' merge strategy.
What Bitbucket see later however is a mystery to me, it recognise the commit as merge but fail to actually merge the branch (does not solve a pull-request) - probably Bitbucket guru could help on this issue, I can not even give you any logs/error messages since I do not have that visibility - git/TortoiseGit does not complain at all though.
精彩评论