设置和使用 Meld 作为你的 git difftool 和 mergetool
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34119866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting up and using Meld as your git difftool and mergetool
提问by mattst
Although much of the information in this question and answer is available on StackOverflow, it is spread out over lots of pages and among other answers which are either wrong or misleading. It took me a while to piece together everything I wanted to know.
尽管此问题和答案中的大部分信息都可以在StackOverflow 上找到,但它散布在许多页面上以及其他错误或误导性的答案中。我花了一段时间才把我想知道的一切拼凑起来。
There are a lot of different programs that can be used as your git difftool and mergetool, and there is certainly no consensus as to which is the best(opinions, requirements, and OSes will clearly differ).
有很多不同的程序可以用作您的 git difftool 和 mergetool,并且对于哪个是最好的肯定没有共识(意见、要求和操作系统会明显不同)。
Meld is a popular free, open-source, and cross-platform (UNIX/Linux, OSX, Windows) choice as shown in the StackOverflowquestion, What's the best visual merge tool for Git?, in which the answer proposing Meld has more than 3 times the votes as any other tool.
Meld 是一种流行的免费、开源和跨平台(UNIX/Linux、OSX、Windows)选择,如StackOverflow问题中所示,Git 的最佳可视化合并工具是什么?,其中提出 Meld 的答案的票数是任何其他工具的 3 倍以上。
The following 2 questions will be answered in my answer below:
以下2个问题将在我的回答中得到解答:
- How do I set up and use Meld as my git difftool?
- How do I set up and use Meld as my git mergetool?
- 如何设置和使用 Meld 作为我的 git difftool?
- 如何设置和使用 Meld 作为我的 git mergetool?
Note: It is not necessary to use the same program as both your difftool and mergetool, different programs can be set for both.
注意:您的 difftool 和 mergetool 不必使用相同的程序,可以为两者设置不同的程序。
回答by mattst
How do I set up and use Meld as my git difftool?
如何设置和使用 Meld 作为我的 git difftool?
git difftooldisplays the diff using a GUI diff program (i.e. Meld) instead of displaying the diff output in your terminal.
git difftool使用 GUI 差异程序(即 Meld)显示差异,而不是在终端中显示差异输出。
Although you can set the GUI program on the command line using -t <tool> / --tool=<tool>
it makes more sense to configure it in your .gitconfig
file. [Note: See the sections about escaping quotes and Windows paths at the bottom.]
虽然您可以在命令行上设置 GUI 程序,-t <tool> / --tool=<tool>
但在您的.gitconfig
文件中配置它更有意义。[注意:请参阅底部有关转义引号和 Windows 路径的部分。]
# Add the following to your .gitconfig file.
[diff]
tool = meld
[difftool]
prompt = false
[difftool "meld"]
cmd = meld "$LOCAL" "$REMOTE"
[Note: These settings will not alter the behaviour of git diff
which will continue to function as usual.]
[注意:这些设置不会改变其行为git diff
将继续照常运行。]
You use git difftool
in exactly the same way as you use git diff
. e.g.
您使用git difftool
的方式与使用git diff
. 例如
git difftool <COMMIT_HASH> file_name
git difftool <BRANCH_NAME> file_name
git difftool <COMMIT_HASH_1> <COMMIT_HASH_2> file_name
If properly configured a Meld window will open displaying the diff using a GUI interface.
如果配置正确,将打开一个 Meld 窗口,使用 GUI 界面显示差异。
The order of the Meld GUI window panes can be controlled by the order of $LOCAL
and $REMOTE
in cmd
, that is to say which file is shown in the left pane and which in the right pane. If you want them the other way around simply swap them around like this:
Meld GUI 窗口窗格的顺序可以由$LOCAL
和$REMOTE
in的顺序控制,cmd
也就是说哪个文件显示在左窗格中,哪个显示在右窗格中。如果你想要它们反过来,只需像这样交换它们:
cmd = meld "$REMOTE" "$LOCAL"
Finally the prompt = false
line simply stops git from prompting you as to whether you want to launch Meld or not, by default git will issue a prompt.
最后,该prompt = false
行只是停止 git 提示您是否要启动 Meld,默认情况下 git 会发出提示。
How do I set up and use Meld as my git mergetool?
如何设置和使用 Meld 作为我的 git mergetool?
git mergetoolallows you to use a GUI merge program (i.e. Meld) to resolve the merge conflicts that have occurred during a merge.
git mergetool允许您使用 GUI 合并程序(即 Meld)来解决合并期间发生的合并冲突。
Like difftool you can set the GUI program on the command line using -t <tool> / --tool=<tool>
but, as before, it makes more sense to configure it in your .gitconfig
file. [Note: See the sections about escaping quotes and Windows paths at the bottom.]
像 difftool 一样,您可以使用命令行在命令行上设置 GUI 程序,-t <tool> / --tool=<tool>
但和以前一样,在您的.gitconfig
文件中配置它更有意义。[注意:请参阅底部有关转义引号和 Windows 路径的部分。]
# Add the following to your .gitconfig file.
[merge]
tool = meld
[mergetool "meld"]
# Choose one of these 2 lines (not both!) explained below.
cmd = meld "$LOCAL" "$MERGED" "$REMOTE" --output "$MERGED"
cmd = meld "$LOCAL" "$BASE" "$REMOTE" --output "$MERGED"
You do NOT use git mergetool
to perform an actual merge. Before using git mergetool
you perform a merge in the usual way with git. e.g.
您不用于git mergetool
执行实际合并。在使用之前,git mergetool
您可以使用 git 以通常的方式执行合并。例如
git checkout master
git merge branch_name
If there is a merge conflict git will display something like this:
如果存在合并冲突,git 将显示如下内容:
$ git merge branch_name
Auto-merging file_name
CONFLICT (content): Merge conflict in file_name
Automatic merge failed; fix conflicts and then commit the result.
At this point file_name
will contain the partially merged file with the merge conflict information (that's the file with all the >>>>>>>
and <<<<<<<
entries in it).
此时file_name
将包含带有合并冲突信息的部分合并文件(即包含所有>>>>>>>
和<<<<<<<
条目的文件)。
Mergetool can now be used to resolve the merge conflicts. You start it very easily with:
Mergetool 现在可用于解决合并冲突。您可以通过以下方式轻松启动:
git mergetool
If properly configured a Meld window will open displaying 3 files. Each file will be contained in a separate pane of its GUI interface.
如果配置正确,将打开一个 Meld 窗口,显示 3 个文件。每个文件都将包含在其 GUI 界面的单独窗格中。
In the example .gitconfig
entry above, 2 lines are suggested as the [mergetool "meld"]
cmd
line. In fact there are all kinds of ways for advanced users to configure the cmd
line, but that is beyond the scope of this answer.
在.gitconfig
上面的示例条目中,建议使用 2 行作为该[mergetool "meld"]
cmd
行。事实上,高级用户可以通过各种方式配置cmd
线路,但这超出了本答案的范围。
This answer has 2 alternative cmd
lines which, between them, will cater for most users, and will be a good starting point for advanced users who wish to take the tool to the next level of complexity.
这个答案有 2 条替代cmd
线,它们之间将满足大多数用户的需求,对于希望将该工具提升到下一个复杂级别的高级用户来说,这将是一个很好的起点。
Firstly here is what the parameters mean:
首先这里是参数的含义:
$LOCAL
is the file in the current branch (e.g. master).$REMOTE
is the file in the branch being merged (e.g. branch_name).$MERGED
is the partially merged file with the merge conflict information in it.$BASE
is the shared commit ancestor of$LOCAL
and$REMOTE
, this is to say the file as it was when the branch containing$REMOTE
was originally created.
$LOCAL
是当前分支中的文件(例如 master)。$REMOTE
是正在合并的分支中的文件(例如 branch_name)。$MERGED
是包含合并冲突信息的部分合并文件。$BASE
是$LOCAL
and的共享提交祖先$REMOTE
,这就是说$REMOTE
最初创建包含的分支时的文件。
I suggest you use either:
我建议你使用:
[mergetool "meld"]
cmd = meld "$LOCAL" "$MERGED" "$REMOTE" --output "$MERGED"
or:
或者:
[mergetool "meld"]
cmd = meld "$LOCAL" "$BASE" "$REMOTE" --output "$MERGED"
# See 'Note On Output File' which explains --output "$MERGED".
The choice is whether to use $MERGED
or $BASE
in between $LOCAL
and $REMOTE
.
选择是使用$MERGED
还是$BASE
在$LOCAL
和之间$REMOTE
。
Either way Meld will display 3 panes with $LOCAL
and $REMOTE
in the left and right panes and either $MERGED
or $BASE
in the middle pane.
无论哪种方式合并将显示3个面板与$LOCAL
和$REMOTE
在左,右窗格,要么$MERGED
或$BASE
在中间窗格中。
In BOTH cases the middle pane is the file that you should edit to resolve the merge conflicts. The difference is just in which starting edit position you'd prefer; $MERGED
for the file which contains the partially merged file with the merge conflict information or $BASE
for the shared commit ancestor of $LOCAL
and $REMOTE
. [Since both cmd
lines can be useful I keep them both in my .gitconfig
file. Most of the time I use the $MERGED
line and the $BASE
line is commented out, but the commenting out can be swapped over if I want to use the $BASE
line instead.]
在这两种情况下,中间窗格是您应该编辑以解决合并冲突的文件。区别仅在于您更喜欢哪个开始编辑位置;$MERGED
对于其中包含与合并冲突信息或部分地合并的文件的文件$BASE
为共享提交的祖先$LOCAL
和$REMOTE
。[因为这两cmd
行都很有用,所以我将它们都保存在我的.gitconfig
文件中。大多数时候我使用该$MERGED
行并且该$BASE
行被注释掉,但是如果我想使用该$BASE
行,可以交换注释掉。]
Note On Output File: Do not worry that --output "$MERGED"
is used in cmd
regardless of whether $MERGED
or $BASE
was used earlier in the cmd
line. The --output
option simply tells Meld what filename git wants the conflict resolution file to be saved in. Meld will save your conflict edits in that file regardless of whether you use $MERGED
or $BASE
as your starting edit point.
注意在输出文件:即不用担心--output "$MERGED"
在使用cmd
无论$MERGED
或$BASE
使用较早的cmd
线路。该--output
选项只是告诉 Meld git 希望将冲突解决文件保存在哪个文件名中。 无论您是使用$MERGED
还是$BASE
作为起始编辑点,Meld 都会将您的冲突编辑保存在该文件中。
After editing the middle pane to resolve the merge conflicts, just save the file and close the Meld window. Git will do the update automatically and the file in the current branch (e.g. master) will now contain whatever you ended up with in the middle pane.
编辑中间窗格以解决合并冲突后,只需保存文件并关闭 Meld 窗口。Git 将自动进行更新,当前分支(例如 master)中的文件现在将包含您在中间窗格中结束的任何内容。
git will have made a backup of the partially merged file with the merge conflict information in it by appending .orig
to the original filename. e.g. file_name.orig
. After checking that you are happy with the merge and running any tests you may wish to do, the .orig
file can be deleted.
git 将通过附加.orig
到原始文件名来备份部分合并的文件,其中包含合并冲突信息。例如file_name.orig
。在检查您对合并是否满意并运行您可能希望执行的任何测试后,.orig
可以删除该文件。
At this point you can now do a commit to commit the changes.
此时,您现在可以执行 commit 以提交更改。
If, while you are editing the merge conflicts in Meld, you wish to abandon the use of Meld, then quit Meld without saving the merge resolution file in the middle pane. git will respond with the message file_name seems unchanged
and then ask Was the merge successful? [y/n]
, if you answer n
then the merge conflict resolution will be aborted and the file will remain unchanged. Note that if you have saved the file in Meld at any point then you will not receive the warning and prompt from git. [Of course you can just delete the file and replace it with the backup .orig
file that git made for you.]
如果在 Meld 中编辑合并冲突时,您希望放弃使用 Meld,则退出 Meld 而不在中间窗格中保存合并解析文件。git 将响应消息file_name seems unchanged
然后询问Was the merge successful? [y/n]
,如果您回答,n
则合并冲突解决将中止,文件将保持不变。请注意,如果您在任何时候将文件保存在 Meld 中,那么您将不会收到来自 git 的警告和提示。[当然你可以删除文件,用.orig
git为你做的备份文件替换它。]
If you have more than 1 file with merge conflicts then git will open a new Meld window for each, one after another until they are all done. They won't all be opened at the same time, but when you finish editing the conflicts in one, and close Meld, git will then open the next one, and so on until all the merge conflicts have been resolved.
如果您有 1 个以上的文件存在合并冲突,那么 git 将为每个文件打开一个新的 Meld 窗口,一个接一个,直到它们全部完成。它们不会同时打开,但是当您完成其中一个冲突的编辑并关闭 Meld 时,git 将打开下一个,依此类推,直到所有合并冲突都已解决。
It would be sensible to create a dummy project to test the use of git mergetool
before using it on a liveproject. Be sure to use a filename containing a space in your test, in case your OS requires you to escape the quotes in the cmd
line, see below.
在将git mergetool
其用于实时项目之前,创建一个虚拟项目来测试其使用是明智的。请确保在测试中使用包含空格的文件名,以防您的操作系统要求您对cmd
行中的引号进行转义,请参见下文。
Escaping quote characters
转义引号字符
Some operating systems may need to have the quotes in cmd
escaped. Less experienced users should remember that config command lines should be tested with filenames that include spaces, and if the cmd
lines don't work with the filenames that include spaces then try escaping the quotes. e.g.
某些操作系统可能需要cmd
转义引号。经验不足的用户应该记住,配置命令行应该使用包含空格的文件名进行测试,如果这些cmd
行不适用于包含空格的文件名,则尝试转义引号。例如
cmd = meld \"$LOCAL\" \"$REMOTE\"
In some cases more complex quote escaping may be needed. The 1st of the Windows path links below contains an example of triple-escaping each quote. It's a bore but sometimes necessary. e.g.
在某些情况下,可能需要更复杂的引号转义。下面的第一个 Windows 路径链接包含一个对每个引号进行三重转义的示例。这很无聊,但有时是必要的。例如
cmd = meld \\"$LOCAL\\" \\"$REMOTE\\"
Windows paths
窗口路径
Windows users will probably need extra configuration added to the Meld cmd
lines. They may need to use the full path to meldc
, which is designed to be called on Windows from the command line, or they may need or want to use a wrapper. They should read the StackOverflowpages linked below which are about setting the correct Meld cmd
line for Windows. Since I am a Linux user I am unable to test the various Windows cmd
lines and have no further information on the subject other than to recommend using my examples with the addition of a full path to Meld or meldc
, or adding the Meld program folder to your path
.
Windows 用户可能需要在 Meldcmd
行中添加额外的配置。他们可能需要使用到 的完整路径meldc
,该路径旨在从命令行在 Windows 上调用,或者他们可能需要或想要使用包装器。他们应该阅读下面链接的StackOverflow页面,这些页面是关于cmd
为 Windows设置正确的 Meld线的。由于我是 Linux 用户,因此我无法测试各种 Windowscmd
行,并且除了建议使用我的示例并添加 Meld 或 的完整路径meldc
,或将 Meld 程序文件夹添加到您的path
.
Ignoring trailing whitespace with Meld
使用 Meld 忽略尾随空格
Meld has a number of preferences that can be configured in the GUI.
Meld 有许多可以在 GUI 中配置的首选项。
In the preferences Text Filters
tab there are several useful filters to ignore things like comments when performing a diff. Although there are filters to ignore All whitespace
and Leading whitespace
, there is no ignore Trailing whitespace
filter (this has been suggested as an addition in the Meld mailing list but is not available in my version).
在首Text Filters
选项选项卡中,有几个有用的过滤器可以在执行差异时忽略诸如注释之类的内容。虽然有过滤器可以忽略All whitespace
和Leading whitespace
,但没有忽略Trailing whitespace
过滤器(这已被建议作为 Meld 邮件列表中的一个补充,但在我的版本中不可用)。
Ignoring trailing whitespace is often very useful, especially when collaborating, and can be manually added easily with a simple regular expression in the Meld preferences Text Filters
tab.
忽略尾随空格通常非常有用,尤其是在协作时,并且可以使用 Meld 首Text Filters
选项选项卡中的简单正则表达式轻松手动添加。
# Use either of these regexes depending on how comprehensive you want it to be.
[ \t]*$
[ \t\r\f\v]*$
I hope this helps everyone.
我希望这对大家有帮助。
回答by Dan Dascalescu
While the other answer is correct, here's the fastest way to just go ahead and configure Meld as your visual diff tool. Just copy/paste this:
虽然另一个答案是正确的,但这是将 Meld 配置为您的视觉差异工具的最快方法。只需复制/粘贴这个:
git config --global diff.tool meld
git config --global difftool.prompt false
Now run git difftool
in a directory and Meld will be launched for each different file.
现在git difftool
在一个目录中运行,将为每个不同的文件启动 Meld。
Side note: Meld is surprisingly slowat comparing CSV files, and no Linux diff tool I've found is faster than this Windows tool called Compare It!(last updated in 2010).
旁注:Meld在比较 CSV 文件时出奇地慢,而且我发现没有一个 Linux diff 工具比这个名为Compare It 的Windows 工具更快!(上次更新于 2010 年)。
回答by MarredCheese
For Windows. Run these commands in Git Bash:
对于 Windows。在 Git Bash 中运行这些命令:
git config --global diff.tool meld
git config --global difftool.meld.path "C:\Program Files (x86)\Meld\Meld.exe"
git config --global difftool.prompt false
git config --global merge.tool meld
git config --global mergetool.meld.path "C:\Program Files (x86)\Meld\Meld.exe"
git config --global mergetool.prompt false
(Update the file path for Meld.exe if yours is different.)
(如果您的路径不同,请更新 Meld.exe 的文件路径。)
For Linux. Run these commands in Git Bash:
对于 Linux。在 Git Bash 中运行这些命令:
git config --global diff.tool meld
git config --global difftool.meld.path "/usr/bin/meld"
git config --global difftool.prompt false
git config --global merge.tool meld
git config --global mergetool.meld.path "/usr/bin/meld"
git config --global mergetool.prompt false
You can verify Meld's path using this command:
您可以使用以下命令验证 Meld 的路径:
which meld
回答by Ulf Adams
I prefer to setup meld as a separate command, like so:
我更喜欢将 meld 设置为单独的命令,如下所示:
git config --global alias.meld '!git difftool -t meld --dir-diff'
This makes it similar to the git-meld.pl script here: https://github.com/wmanley/git-meld
这使它类似于此处的 git-meld.pl 脚本:https: //github.com/wmanley/git-meld
You can then just run
然后你可以运行
git meld
回答by Jeremy Benks
For Windows 10 I had to put this in my .gitconfig:
对于 Windows 10,我必须把它放在我的 .gitconfig 中:
[merge]
tool = meld
[mergetool "meld"]
cmd = 'C:/Program Files (x86)/Meld/Meld.exe' $LOCAL $BASE $REMOTE --output=$MERGED
[mergetool]
prompt = false
Everything else you need to know is written in this super answerby mattst further above.
您需要知道的其他所有内容都写在上面由 mattst撰写的超级答案中。
PS: For some reason, this only worked with Meld 3.18.x, Meld 3.20.x gives me an error.
PS:出于某种原因,这只适用于 Meld 3.18.x,Meld 3.20.x 给了我一个错误。
回答by Tore Aurstad
This is an answer targeting primarily developers using Windows, as the path syntax of the diff tool differs from other platforms.
这是一个主要针对使用 Windows 的开发人员的答案,因为 diff 工具的路径语法与其他平台不同。
I use Kdiff3 as the git mergetool, but to set up the git difftool as Meld, I first installed the latest version of Meld from Meldmerge.orgthen added the following to my global .gitconfig using:
我使用 Kdiff3 作为 git mergetool,但要将 git difftool 设置为 Meld,我首先从Meldmerge.org安装了最新版本的 Meld, 然后使用以下命令将以下内容添加到我的全局 .gitconfig 中:
git config --global -e
Note, if you rather want Sublime Text 3 instead of the default Vim as core ditor, you can add this to the .gitconfig file:
请注意,如果您希望 Sublime Text 3 而不是默认的 Vim 作为核心编辑器,您可以将其添加到 .gitconfig 文件中:
[core]
editor = 'c:/Program Files/Sublime Text 3/sublime_text.exe'
Then you add inn Meld as the difftool
然后你添加 inn Meld 作为 difftool
[diff]
tool = meld
guitool = meld
[difftool "meld"]
cmd = \"C:/Program Files (x86)/Meld/Meld.exe\" \"$LOCAL\" \"$REMOTE\" --label \"DIFF
(ORIGINAL MY)\"
prompt = false
path = C:\Program Files (x86)\Meld\Meld.exe
Note the leading slash in the cmd above, on Windows it is necessary.
注意上面 cmd 中的前导斜杠,在 Windows 上是必要的。
It is also possible to set up an alias to show the current git diff with a --dir-diffoption. This will list the changed files inside Meld, which is handy when you have altered multiple files (a very common scenario indeed).
也可以设置别名以使用--dir-diff选项显示当前的 git diff 。这将列出 Meld 中已更改的文件,这在您更改多个文件时非常方便(确实是一种非常常见的情况)。
The alias looks like this inside the .gitconfig file, beneath [alias]section:
.gitconfig 文件中的别名如下所示,位于[alias]部分下方:
showchanges = difftool --dir-diff
To show the changes I have made to the code I then just enter the following command:
为了显示我对代码所做的更改,我只需输入以下命令:
git showchanges
The following image shows how this --dir-diff option can show a listing of changed files (example):
下图显示了此 --dir-diff 选项如何显示已更改文件的列表(示例):
Then it is possible to click on each file and show the changes inside Meld.
然后可以单击每个文件并显示 Meld 内部的更改。
回答by mnieber
It can be complicated to compute a diff in your head from the different sections in $MERGED and apply that. In my setup, meld helps by showing you these diffs visually, using:
从 $MERGED 中的不同部分计算您头脑中的差异并应用它可能很复杂。在我的设置中,meld 通过使用以下方法直观地向您显示这些差异来提供帮助:
[merge]
tool = mymeld
conflictstyle = diff3
[mergetool "mymeld"]
cmd = meld --diff $BASE $REMOTE --diff $REMOTE $LOCAL --diff $LOCAL $MERGED
It looks strange but offers a very convenient work-flow, using three tabs:
它看起来很奇怪,但使用三个选项卡提供了非常方便的工作流程:
in tab 1 you see (from left to right) the change that you should make in tab 2 to solve the merge conflict.
in the right side of tab 2 you apply the "change that you should make" and copy the entire file contents to the clipboard (using ctrl-a and ctrl-c).
in tab 3 replace the right side with the clipboard contents. If everything is correct, you will now see - from left to right - the same change as shown in tab 1 (but with different contexts). Save the changes made in this tab.
在选项卡 1 中,您会看到(从左到右)您应该在选项卡 2 中进行的更改以解决合并冲突。
在选项卡 2 的右侧,您应用“您应该进行的更改”并将整个文件内容复制到剪贴板(使用 ctrl-a 和 ctrl-c)。
在选项卡 3 中,用剪贴板内容替换右侧。如果一切正常,您现在将看到 - 从左到右 - 与选项卡 1 中显示的更改相同(但上下文不同)。保存在此选项卡中所做的更改。
Notes:
笔记:
- don't edit anything in tab 1
- don't save anything in tab 2 because that will produce annoying popups in tab 3
- 不要编辑选项卡 1 中的任何内容
- 不要在选项卡 2 中保存任何内容,因为这会在选项卡 3 中产生烦人的弹出窗口