问题
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.
回答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.
回答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
回答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.
回答4:
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.
回答5:
For anyone who came here and is now wondering about the difference between @True's answer using git difftool
and the other answers that use git merge
, see Git mergetool vs difftool.
In short, if you have git configured to use a modern diff.tool
such as kdiff3, meld, or vimdiff, you'll be able to manually merge differing files using the diff tool itself, 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
).
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.)
Someone might jump in to correct me, but other than the N-way merge ability, the two methods appear to produce the same result. Neither difftool
nor mergetool
adds other_branch as a parent on the new commit, so in both cases, the merge isn't obvious in e.g. gitk and would have to be described (and noticed later) in the commit message.
回答6:
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.
来源:https://stackoverflow.com/questions/4657009/how-to-merge-all-files-manually-in-git