设置和使用Meld作为你的git difftool和mergetool [英] Setting up and using Meld as your git difftool and mergetool

查看:217
本文介绍了设置和使用Meld作为你的git difftool和mergetool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然这个问题和答案中的大部分信息都可以在StackOverflow上找到,但它分布在很多页面上,以及其他答案都是错误的或者误导性的。我花了一段时间来拼凑我想知道的所有东西。

有很多不同的程序可以用作你的git difftool和mergetool,还有关于哪个是最好的(关于意见,要求和操作系统明显不同),肯定没有达成共识。



Meld是一个流行的跨平台(UNIX / Linux,OSX,Windows)的选择,如 StackOverflow 问题所示,,其中提出Meld的答案比其他工具的投票次数多3倍。



以下2个问题将在我的回答中回答:


  • 如何设置和使用Meld作为我的git difftool?

  • 如何设置并使用Meld作为我的git mergetool?
  • ul>

    注意:没有必要使用与您的di相同的程序fftool和mergetool,可以为两者设置不同的程序。

    解决方案

    如何设置并使用Meld作为我的git difftool?



    git difftool 显示使用GUI差异程序(即Meld)而不是在终端中显示diff输出。

    尽管您可以使用 -t< tool>设置GUI程序, / --tool =< tool> 将它配置在 .gitconfig 文件中更有意义。 [注意:请参阅底部的转义引号和Windows路径章节。]

     #将以下内容添加到.gitconfig文件中。 
    [diff]
    tool = meld
    [difftool]
    prompt = false
    [difftoolmeld]
    cmd = meld$ LOCAL $ REMOTE

    [注意:这些设置不会改变 git的行为diff 它会继续照常运行。]



    您可以使用 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

    如果配置正确,Meld窗口将打开,并使用GUI界面显示diff。



    Meld GUI窗口窗格的顺序可以通过 $ LOCAL $ REMOTE 在 cmd 中,也就是说哪个文件显示在左窗格中,哪个文件显示在右窗格中。如果你想要他们,只需将它们换成这样:

      cmd = meld$ REMOTE$ LOCAL 

    最后, prompt = false git从提示您是否要启动Meld,默认情况下git会发出提示。






    设置并使用Meld作为我的git mergetool?



    git mergetool 允许您使用GUI合并程序(即Meld)来解决合并期间发生的合并冲突。



    喜欢difftool,您可以使用 -t< tool>设置GUI程序/ --tool =< tool> 但与以前一样,将它配置在 .gitconfig 文件中更有意义。 [注意:请参阅底部的转义引号和Windows路径章节。]

     #将以下内容添加到.gitconfig文件中。 
    [merge]
    tool = meld
    [mergetoolmeld]
    #选择下面解释的这2行中的一行(不是两行!)。
    cmd = meld$ LOCAL$ MERGED$ REMOTE--output$ MERGED
    cmd = meld$ LOCAL$ BASE$ REMOTE--output$合并

    您不使用 git mergetool 执行实际合并。在使用 git mergetool 之前,你需要用git以通常的方式执行合并。例如

      git checkout master 
    git merge branch_name

    如果发生合并冲突,git将显示如下所示:

      $ git merge branch_name 
    自动合并file_name
    CONFLICT(内容):在file_name中合并冲突
    自动合并失败;修复冲突,然后提交结果。

    此时 file_name 将包含部分合并文件与合并冲突信息(即所有>>>>>>> <



    现在可以使用Mergetool来解决合并冲突。

    你可以很容易地开始:

      git mergetool 

    如果配置正确,Meld窗口将打开显示3个文件。每个文件都将包含在其GUI界面的单独窗格中。



    在上面的示例 .gitconfig 建议使用2行作为 [mergetoolmeld] cmd 行。实际上,高级用户可以通过各种方式配置 cmd 行,但这超出了本答案的范围。



    这个答案有两个可供选择的 cmd 行,这些行可以满足大多数用户的需求,对于希望获得高级用户这个工具到了下一个复杂程度。

    首先这里是参数的意思:

    $ ul

  • $ LOCAL 是当前分支中的文件(例如master)。
  • $ REMOTE 是合并分支中的文件(例如branch_name)。
  • $ MERGED 是部分合并的其中包含合并冲突信息。

  • $ BASE $ LOCAL的共享提交祖先 $ REMOTE ,这就是说当文件包含 $ REMOTE 最初创建。



我建议您使用eith er:

  [mergetoolmeld] 
cmd = meld$ LOCAL$ MERGED$ REMOTE --output$ MERGED

或者:

  [mergetoolmeld] 
cmd = meld$ LOCAL$ BASE$ REMOTE--output$ MERGED

选择是否使用 $ MERGED 或<$ c $ $ LOCAL $ REMOTE

$ c> $ BASE
b
$ b

无论哪种方式,Meld将显示3个窗格,其中 $ LOCAL $ REMOTE in左窗格和右窗格以及中间窗格中的 $ MERGED $ BASE



在这两种情况下,中间窗格是您应该编辑以解决合并冲突的文件。区别在于你喜欢哪个开始编辑位置; $ MERGED 用于包含合并冲突信息的部分合并文件的文件或共享提交祖先的 $ BASE $ LOCAL $ REMOTE 。 [既然 cmd 行都很有用,我将它们都保存在我的 .gitconfig 文件中。大多数情况下,我使用 $ MERGED 行,并且 $ BASE 行被注释掉,但注释掉可以如果我想使用 $ BASE 行来替换,就可以交换。]



编辑中间窗格以解决合并冲突,只保存文件并关闭Meld窗口。 Git会自动执行更新,当前分支(例如master)中的文件现在将包含您在中间窗格中结束的任何内容。



git将会创建一个通过将 .orig 附加到原始文件名中,备份部分合并的文件并在其中包含合并冲突信息。例如 file_name.orig 。在检查完合并并运行任何可能希望执行的测试后,可以删除 .orig 文件。



此时,您现在可以进行提交以提交更改。



注意:不要担心 - 输出无论是 $ MERGED 还是<$ c $都使用$ MERGED用于 cmd c> $ BASE 早先在 cmd 行中使用过。 - output 选项只是告诉Meld git需要保存冲突解决文件的文件名。Meld将确保您的冲突编辑保存在该文件中,而不管是否您使用 $ MERGED $ BASE 作为您的编辑开始点。



如果在编辑Meld中的合并冲突时,您希望放弃使用Meld,然后退出Meld而不将合并解析文件保存在中间窗格中。 git会以消息 file_name似乎不变来回应,然后询问合并成功了吗? [y / n] ,如果您回答 n ,则合并冲突解决方案将被中止并且文件保持不变。请注意,如果您在任何时候已将文件保存在Meld中,那么您将不会收到来自git的警告和提示。 [当然,你可以删除这个文件,并用git替你备份的 .orig 文件替换它。]



如果你有超过1个合并冲突的文件,那么git会为每个文件一个接一个地打开一个新的Meld窗口,直到它们全部完成。他们不会同时被打开,但是当你完成一个冲突的编辑,然后关闭Meld时,git会打开下一个,依此类推,直到所有合并冲突得到解决。



创建一个虚拟项目来测试 git mergetool 在使用 live 项目。请确保在测试中使用包含空格的文件名,以防操作系统要求您转义 cmd 行中的引号,如下所示。






转义引号字符



某些操作系统可能需要将引号放在 cmd 已转义。缺乏经验的用户应该记住,配置命令行应该使用包含空格的文件名进行测试,如果 cmd 行不能与包含空格的文件名一起使用,那么请尝试转义引号。例如

  cmd = meld \$ LOCAL \\$ REMOTE \
code>

在某些情况下,可能需要更复杂的引号转义。下面的第一个Windows路径链接包含每个报价三次转义的示例。这是一个缺点,但有时是必要的。例如

  cmd = meld \\\\$ LOCAL \\\\\\ $ REMOTE \\\\



Windows路径



Windows用户可能需要将额外配置添加到Meld cmd 行中。他们可能需要使用 meldc 的完整路径,该路径旨在从命令行在Windows上调用,或者他们可能需要或希望使用包装器。他们应该阅读下面链接的 StackOverflow 页面,其中介绍了如何为Windows设置正确的Meld cmd 行。由于我是Linux用户,因此我无法测试各种Windows cmd 行,并且没有关于该主题的更多信息,除了推荐使用我的示例以及添加完整到Meld或 meldc 的路径,或者将Meld程序文件夹添加到路径





忽略Meld的尾部空白



<



在首选项文本过滤器标签页中,有几个有用的过滤器可以忽略瘦身gs在执行差异时喜欢评论。虽然有过滤器可以忽略所有空格前导空格,但不会忽略尾随空格过滤器(这在Meld邮件列表中被建议添加,但在我的版本中不可用)。



忽略尾随空格非常有用,特别是在协作时,可以通过Meld首选项文本过滤器选项卡中的简单正则表达式轻松手动添加。

 #使用这些正则表达式中的任何一种,具体取决于您希望得到多么全面。 
[\t] * $
[\t\r\f\v] *






我希望这有助于每个人。


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.

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).

Meld is a popular cross-platform (UNIX/Linux, OSX, Windows) choice as shown in the StackOverflow question, 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.

The following 2 questions will be answered in my answer below:

  • How do I set up and use Meld as my git difftool?
  • How do I set up and use Meld as my 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.

解决方案

How do I set up and use Meld as my git difftool?

git difftool displays the diff using a GUI diff program (i.e. Meld) instead of displaying the diff output in your terminal.

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.]

# 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.]

You use git difftool in exactly the same way as you use git diff. e.g.

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.

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:

    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.


How do I set up and use Meld as my git mergetool?

git mergetool allows you to use a GUI merge program (i.e. Meld) to resolve the merge conflicts that have occurred during a merge.

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.]

# 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 checkout master
git merge branch_name

If there is a merge conflict git will display something like this:

$ 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).

Mergetool can now be used to resolve the merge conflicts. You start it very easily with:

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.

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.

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.

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.

I suggest you use either:

[mergetool "meld"]
    cmd = meld "$LOCAL" "$MERGED" "$REMOTE" --output "$MERGED"

or:

[mergetool "meld"]
    cmd = meld "$LOCAL" "$BASE" "$REMOTE" --output "$MERGED"

The choice is whether to use $MERGED or $BASE in between $LOCAL and $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.

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.]

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.

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.

At this point you can now do a commit to commit the changes.

NOTE: 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 make sure that your conflict edits are saved in that file regardless of whether you use $MERGED or $BASE as your starting edit point.

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.]

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.

It would be sensible to create a dummy project to test the use of git mergetool before using it on a live project. 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.


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 = 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.

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 StackOverflow pages 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.

Ignoring trailing whitespace with Meld

Meld has a number of preferences that can be configured in the 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).

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.

# Use either of these regexes depending on how comprehensive you want it to be.
[ \t]*$
[ \t\r\f\v]*$


I hope this helps everyone.

这篇关于设置和使用Meld作为你的git difftool和mergetool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆