我如何查看git diff'使用我喜欢的diff工具/查看器输出?

发布于 2025-02-03 08:34:02 字数 108 浏览 4 评论 0 原文

当我键入 git diff 时,我想使用我选择的Visual diff工具(Windows上的sourcegear“ diffmerge”)查看输出。如何配置git做到这一点?

When I type git diff, I want to view the output with my visual diff tool of choice (SourceGear "diffmerge" on Windows). How do I configure git to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(26

羁客 2025-02-10 08:34:03

这是一个适用于Windows的批处理文件 - 假设在默认位置安装了Diffmerge(处理X64),并根据需要将其前进到后斜线更换,并且具有安装自身的能力。用您喜欢的DIFF程序替换Diffmerge应该很容易。

安装:

gitvdiff --install 

gitvdiff.bat:

@echo off

REM ---- Install? ----
REM To install, run gitvdiff --install

if %1==--install goto install



REM ---- Find DiffMerge ----

if DEFINED ProgramFiles^(x86^) (
    Set DIFF="%ProgramFiles(x86)%\SourceGear\DiffMerge\DiffMerge.exe"
) else (
    Set DIFF="%ProgramFiles%\SourceGear\DiffMerge\DiffMerge.exe"
)



REM ---- Switch forward slashes to back slashes ----

set oldW=%2
set oldW=%oldW:/=\%
set newW=%5
set newW=%newW:/=\%


REM ---- Launch DiffMerge ----

%DIFF% /title1="Old Version" %oldW% /title2="New Version" %newW%

goto :EOF



REM ---- Install ----
:install
set selfL=%~dpnx0
set selfL=%selfL:\=/%
@echo on
git config --global diff.external %selfL%
@echo off


:EOF

Here's a batch file that works for Windows - assumes DiffMerge installed in default location, handles x64, handles forward to backslash replacement as necessary and has ability to install itself. Should be easy to replace DiffMerge with your favourite diff program.

To install:

gitvdiff --install 

gitvdiff.bat:

@echo off

REM ---- Install? ----
REM To install, run gitvdiff --install

if %1==--install goto install



REM ---- Find DiffMerge ----

if DEFINED ProgramFiles^(x86^) (
    Set DIFF="%ProgramFiles(x86)%\SourceGear\DiffMerge\DiffMerge.exe"
) else (
    Set DIFF="%ProgramFiles%\SourceGear\DiffMerge\DiffMerge.exe"
)



REM ---- Switch forward slashes to back slashes ----

set oldW=%2
set oldW=%oldW:/=\%
set newW=%5
set newW=%newW:/=\%


REM ---- Launch DiffMerge ----

%DIFF% /title1="Old Version" %oldW% /title2="New Version" %newW%

goto :EOF



REM ---- Install ----
:install
set selfL=%~dpnx0
set selfL=%selfL:\=/%
@echo on
git config --global diff.external %selfL%
@echo off


:EOF
み格子的夏天 2025-02-10 08:34:03

如果您在Mac上并且具有 xcode ,那么您就有 filemerge 已安装。终端命令是opendiff,所以您可以做:

git difftool -t opendiff

If you're on a Mac and have Xcode, then you have FileMerge installed. The terminal command is opendiff, so you can just do:

git difftool -t opendiff
像极了他 2025-02-10 08:34:03

引用的简介

,我想在 vonc的答案。请记住,我正在使用修改后的路径使用MSYS版本(目前为1.6.0.2),并从PowerShell(或CMD.EXE)(而不是bash shell)运行git本身。

我引入了一个新命令, gitdiff 。运行此命令暂时重定向 git diff 使用您选择的Visual diff程序(与永久执行的VONC解决方案相反)。这使我同时拥有默认的git diff功能( git diff )以及Visual diff功能( gitdiff )。这两个命令都采用相同的参数,因此,例如,要在特定文件中视觉上的差异更改,您可以输入

gitdiff path/file.txt

设置

注意,请注意, $ gitinstall 被用作安装git的目录的占位符。

  1. 创建一个新文件, $ gitinstall \ cmd \ gitdiff.cmd

      @echo关闭
     setLocal
     for /f“ delims =” %% i in(“%〜dp0 ..”)do @set path = %% 〜fi \ bin; %% 〜fi \ mingw \ bin;%路径%
     如果“%home%” ==“” @set home =%userProfile%
     设置git_external_diff = git-diff-visual.cmd
     设置git_pager = cat
     git diff%*
     局部
     
  2. 创建一个新文件, $ gitinstall \ bin \ bin \ git-diff-visual.cmd.cmd 代码> [visual_diff_exe] 占位符,带有您选择的DIFF程序的完整路径)

      @echo关闭
     REM DIFF通过7个参数来调用GIT:
     REM路径旧文件旧模式新文件新模式新模式
     回声差异“%5”
     “ [visual_diff_exe]”“%2”“%5”
     出口0
     
  3. 您现在完成了。从git存储库中运行 gitdiff 现在应为已更改的每个文件调用您的Visual diff程序。

Introduction

For reference I'd like to include my variation on VonC's answer. Keep in mind that I am using the MSys version of Git (1.6.0.2 at this time) with modified PATH, and running Git itself from PowerShell (or cmd.exe), not the Bash shell.

I introduced a new command, gitdiff. Running this command temporarily redirects git diff to use a visual diff program of your choice (as opposed to VonC's solution that does it permanently). This allows me to have both the default Git diff functionality (git diff) as well as visual diff functionality (gitdiff). Both commands take the same parameters, so for example to visually diff changes in a particular file you can type

gitdiff path/file.txt

Setup

Note that $GitInstall is used as a placeholder for the directory where Git is installed.

  1. Create a new file, $GitInstall\cmd\gitdiff.cmd

     @echo off
     setlocal
     for /F "delims=" %%I in ("%~dp0..") do @set path=%%~fI\bin;%%~fI\mingw\bin;%PATH%
     if "%HOME%"=="" @set HOME=%USERPROFILE%
     set GIT_EXTERNAL_DIFF=git-diff-visual.cmd
     set GIT_PAGER=cat
     git diff %*
     endlocal
    
  2. Create a new file, $GitInstall\bin\git-diff-visual.cmd (replacing [visual_diff_exe] placeholder with full path to the diff program of your choice)

     @echo off
     rem diff is called by git with 7 parameters:
     rem path old-file old-hex old-mode new-file new-hex new-mode
     echo Diffing "%5"
     "[visual_diff_exe]" "%2" "%5"
     exit 0
    
  3. You're now done. Running gitdiff from within a Git repository should now invoke your visual diff program for every file that was changed.

凤舞天涯 2025-02-10 08:34:03

对于Linux版本,如何在1.6.3之前在GIT版本上配置DIFF工具(1.6.3添加到Git中),这是一个很棒的简洁教程。

[diff]
  external = git_diff_wrapper
[pager]
  diff =

#!/bin/sh

vimdiff "$2" "$5"

For a Linux version of how to configure a diff tool on Git versions prior to 1.6.3 (1.6.3 added difftool to Git), this is a great concise tutorial.

In brief:

Step 1: add this to your .gitconfig

[diff]
  external = git_diff_wrapper
[pager]
  diff =

Step 2: create a file named git_diff_wrapper, put it somewhere in your $PATH

#!/bin/sh

vimdiff "$2" "$5"
愁杀 2025-02-10 08:34:03

在Mac OS X上,

git difftool -t diffuse

在Git文件夹中为我完成了工作。用于安装 diffuse ,一个人可以使用端口 -

sudo port install diffuse

On Mac OS X,

git difftool -t diffuse

does the job for me in the Git folder. For installing Diffuse, one can use port -

sudo port install diffuse
童话里做英雄 2025-02-10 08:34:03

可以从这里的其他答案中收集以下内容,但是对我来说,这很困难(太多信息),因此,这是tkdiff的“ Just In tot It”答案:

git difftool --tool=tkdiff <path to the file to be diffed>

您可以将您喜欢的扩散工具的可执行名称替换为TKDIFF。只要(例如TKDIFF)(或您最喜欢的扩散工具)在您的路径中,它将被启动。

The following can be gleaned from the other answers here, but for me it's difficult, (too much information), so here's the 'just type it in' answer for tkdiff:

git difftool --tool=tkdiff <path to the file to be diffed>

You can substitute the executable name of your favorite diffing tool for tkdiff. As long as (e.g. tkdiff), (or your favorite diffing tool) is in your PATH, it will be launched.

偏闹i 2025-02-10 08:34:03

您可以使用 git difftool

例如,如果您有 meld ,您可以编辑分支 master DEVEL

git config --global diff.external meld
git difftool master..devel

You can use git difftool.

For example, if you have Meld, you can edit the branches master and devel by:

git config --global diff.external meld
git difftool master..devel
_蜘蛛 2025-02-10 08:34:03

我在这里尝试了花哨的东西(与TKDIFF一起),对我没有任何帮助。因此,我写了以下脚本Tkgitdiff。它可以做我需要做的事情。

$ cat tkgitdiff
#!/bin/sh

#
# tkdiff for git.
# Gives you the diff between HEAD and the current state of your file.
#

newfile=$1
git diff HEAD -- $newfile > /tmp/patch.dat
cp $newfile /tmp
savedPWD=$PWD
cd /tmp
patch -R $newfile < patch.dat
cd $savedPWD
tkdiff /tmp/$newfile $newfile

I tried the fancy stuff here (with tkdiff) and nothing worked for me. So I wrote the following script, tkgitdiff. It does what I need it to do.

$ cat tkgitdiff
#!/bin/sh

#
# tkdiff for git.
# Gives you the diff between HEAD and the current state of your file.
#

newfile=$1
git diff HEAD -- $newfile > /tmp/patch.dat
cp $newfile /tmp
savedPWD=$PWD
cd /tmp
patch -R $newfile < patch.dat
cd $savedPWD
tkdiff /tmp/$newfile $newfile
七颜 2025-02-10 08:34:03

很长一段时间以来,我一直在文件〜/.gitConfig 中使用此位:

[diff]
    external = ~/Dropbox/source/bash/git-meld

使用 git-Meld-Meld

#!/bin/bash
if [ "$DISPLAY" = "" ];
then
    diff $2 $5
else
    meld $2 $5
fi

但是现在我厌倦了总是使用 meld 在图形环境中,使用此设置调用正常的差异并不是很微不足道的,所以我切换到此设置:

[alias]
    v =  "!sh -c 'if [ $# -eq 0 ] ; then git difftool -y -t meld ; else git difftool -y $@ ; fi' -"

使用此设置,东西,东西,东西,东西像这样的工作:

git v
git v --staged
git v -t kompare
git v --staged -t tkdiff

我仍然可以保留良好的旧 git diff

I've been using this bit in file ~/.gitconfig for a long time:

[diff]
    external = ~/Dropbox/source/bash/git-meld

With git-meld:

#!/bin/bash
if [ "$DISPLAY" = "" ];
then
    diff $2 $5
else
    meld $2 $5
fi

But now I got tired of always using Meld in a graphical environment, and it's not trivial to invoke the normal diff with this setup, so I switched to this:

[alias]
    v =  "!sh -c 'if [ $# -eq 0 ] ; then git difftool -y -t meld ; else git difftool -y $@ ; fi' -"

With this setup, things like this work:

git v
git v --staged
git v -t kompare
git v --staged -t tkdiff

And I still get to keep the good old git diff.

轻拂→两袖风尘 2025-02-10 08:34:03

我使用 kompare 在ubuntu上:

sudo apt-get install kompare

比较两个分支:

git difftool -t kompare <my_branch> master

I use Kompare on Ubuntu:

sudo apt-get install kompare

To compare two branches:

git difftool -t kompare <my_branch> master
江湖彼岸 2025-02-10 08:34:03

如果您碰巧已经有一个与Filetypes关联的DIFF工具(例如,因为您安装了带有diff查看器的totoisesvn),则可以将常规 git diff 输出输出到“ temp”文件,然后只需直接打开该文件而无需了解有关查看器的任何信息:

git diff > "~/temp.diff" && start "~/temp.diff"

将其设置为全局别名的工作效果更好: git what what

[alias]
    what = "!f() { git diff > "~/temp.diff" && start "~/temp.diff"; }; f"

If you happen to already have a diff tool associated with filetypes (say, because you installed TortoiseSVN which comes with a diff viewer) you could just pipe the regular git diff output to a "temp" file, then just open that file directly without needing to know anything about the viewer:

git diff > "~/temp.diff" && start "~/temp.diff"

Setting it as a global alias works even better: git what

[alias]
    what = "!f() { git diff > "~/temp.diff" && start "~/temp.diff"; }; f"
静谧幽蓝 2025-02-10 08:34:03

如果您不是命令行之一在文件上以使用“以后的diff”选项获取totoisegit子菜单。

当您在第一个文件上选择此内容时,您可以右键单击第二个文件,转到Tortoisegit子菜单,然后选择“ diff with == yourfilehere ==”。
这将为结果提供乌龟合并GUI。

If you're not one for the command line then if you install TortoiseGit, you can right click on a file to get a TortoiseGit submenu with the "Diff later" option.

When you select this on the first file, you can then right click on the second file, go to the TortoiseGit submenu and select "Diff with ==yourfilehere==".
This will give the TortoiseGit merge GUI for the result.

说好的呢 2025-02-10 08:34:03

您可能需要尝试 xd ,这是Git/svn diff的GUI包装器。它不是 一个差异工具本身。

您运行 XD 要运行 git diff svn diff ,它将显示一个文件列表,一个预览窗口,您可以启动您喜欢的任何差异工具,包括tkdiff,xxdiff,gvimdiff,emacs(ediff), meld diffuse kompare“> kompare”> kompare http://kdiff3.sourceforge.net/“ rel =“ nofollow noreferrer”> kdiff3 。您也可以运行任何自定义工具。

不幸的是,该工具不支持Windows。

披露:我是该工具的作者。

You may want to try out xd, which is a GUI wrapper for Git/SVN diff. It is not a diff tool itself.

You run xd when you want to run git diff or svn diff and it will show you a list of files, a preview window and you can launch any diff tool you like, including tkdiff, xxdiff, gvimdiff, Emacs (ediff), XEmacs (ediff), Meld, Diffuse, Kompare and KDiff3. You can also run any custom tool.

Unfortunately the tool doesn't support Windows.

Disclosure: I am the author of this tool.

风吹雪碎 2025-02-10 08:34:02

由于Git1.6.3,您可以使用 git difftool脚本:请参阅我的答案)。


可能是此文章将为您提供帮助。这是最好的部分:

有两种不同的方法来指定外部差异工具。

第一个是通过设置GIT_EXTERNAL_DIFF变量来使用的方法。但是,该变量应该指向可执行文件的完整路径。此外,Git_external_diff指定的可执行文件将使用固定的7个参数调用:

path old-file old-hex old-mode new-file new-hex new-mode

由于大多数DIFF工具都需要不同的顺序(并且只有某些参数),因此您很可能必须指定包装脚本转到真正的差异工具。

我更喜欢的第二种方法是通过“ git配置外部差异工具
config“
。这是我所做的:

1)创建一个包装脚本“ git-diff-wrapper.sh”,其中包含类似的内容

-->8-(snip)--
#!/bin/sh

# diff is called by git with 7 parameters:
# path old-file old-hex old-mode new-file new-hex new-mode

"<path_to_diff_executable>" "$2" "$5" | cat
--8<-(snap)--

,只有第二个(“ old-file”)和第五个( “新文件”)论点将是
传递给差异工具。

2)

$ git config --global diff.external <path_to_wrapper_script>

在命令提示符下键入“ git-diff-wrapper.sh”的路径,因此您的〜/.gitConfig包含

-->8-(snip)--
[diff]
    external = <path_to_wrapper_script>
--8<-(snap)--

确保使用正确的语法来指定包装脚本和diff的路径
工具,即使用前向斜线而不是后斜线。就我而言,我有

[diff]
    external = \"c:/Documents and Settings/sschuber/git-diff-wrapper.sh\"

.gitConfig和

"d:/Program Files/Beyond Compare 3/BCompare.exe" "$2" "$5" | cat

包装脚本。介意尾随的“猫”!

(我想仅对于某些可能无法返回适当或一致的返回状态的程序才需要' | cat '。如果您的diff工具具有明确的返回状态,则可能需要在没有拖延猫的情况下尝试)

diomidis spinellis 添加在注释中

需要 cat 命令,因为 diff(1) ,默认情况下,如果文件有所不同,则使用错误代码退出。
GIT期望仅在发生实际错误时使用错误代码退出外部DIFF程序,例如,如果它用完了内存。
通过将 git 的输出输送到 CAT 屏蔽了非零错误代码。
更有效地,该程序可以使用 exit 和0的参数。)


更有效地,该 环境变量)。
实际上(对于配置文件的定义,外部工具的定义),您可以参考:

Since Git1.6.3, you can use the git difftool script: see my answer below.


May be this article will help you. Here are the best parts:

There are two different ways to specify an external diff tool.

The first is the method you used, by setting the GIT_EXTERNAL_DIFF variable. However, the variable is supposed to point to the full path of the executable. Moreover, the executable specified by GIT_EXTERNAL_DIFF will be called with a fixed set of 7 arguments:

path old-file old-hex old-mode new-file new-hex new-mode

As most diff tools will require a different order (and only some) of the arguments, you will most likely have to specify a wrapper script instead, which in turn calls the real diff tool.

The second method, which I prefer, is to configure the external diff tool via "git
config"
. Here is what I did:

1) Create a wrapper script "git-diff-wrapper.sh" which contains something like

-->8-(snip)--
#!/bin/sh

# diff is called by git with 7 parameters:
# path old-file old-hex old-mode new-file new-hex new-mode

"<path_to_diff_executable>" "$2" "$5" | cat
--8<-(snap)--

As you can see, only the second ("old-file") and fifth ("new-file") arguments will be
passed to the diff tool.

2) Type

$ git config --global diff.external <path_to_wrapper_script>

at the command prompt, replacing with the path to "git-diff-wrapper.sh", so your ~/.gitconfig contains

-->8-(snip)--
[diff]
    external = <path_to_wrapper_script>
--8<-(snap)--

Be sure to use the correct syntax to specify the paths to the wrapper script and diff
tool, i.e. use forward slashed instead of backslashes. In my case, I have

[diff]
    external = \"c:/Documents and Settings/sschuber/git-diff-wrapper.sh\"

in .gitconfig and

"d:/Program Files/Beyond Compare 3/BCompare.exe" "$2" "$5" | cat

in the wrapper script. Mind the trailing "cat"!

(I suppose the '| cat' is needed only for some programs which may not return a proper or consistent return status. You might want to try without the trailing cat if your diff tool has explicit return status)

(Diomidis Spinellis adds in the comments:

The cat command is required, because diff(1), by default exits with an error code if the files differ.
Git expects the external diff program to exit with an error code only if an actual error occurred, e.g. if it run out of memory.
By piping the output of git to cat the non-zero error code is masked.
More efficiently, the program could just run exit with and argument of 0.)


That (the article quoted above) is the theory for external tool defined through config file (not through environment variable).
In practice (still for config file definition of external tool), you can refer to:

泪之魂 2025-02-10 08:34:02

要完成我以前的

​/901386#901386“> jakub ,git1.6.3引入 difftool ,最初于2008年9月提出:

usage = '[ - tool = tool = tool] [ - commit = ref] [-start = ref - ref - end = - end = ref] [ - no-prompt] [file to Merge]'
(请参阅 - 本答案的最后一部分中的ExtCMD

$ local 包含来自启动修订版中文件的内容, $ $远程包含末尾修订中文件的内容。
$ base 在WOR中包含文件的内容

基本上是 git-mergetool 已修改以在git索引/worktree上操作。

此脚本的通常用例是您进行了上演或未分段的更改,您想查看并排的diff查看器中的更改(例如 xxdiff tkdiff 等)。

git difftool [<filename>*]

另一个用例是您想查看相同的信息,但正在比较任意提交(这是续续解析可能更好的部分)

git difftool --start=HEAD^ --end=HEAD [-- <filename>*]

最后一个用例是您想将当前的工作者与其他事物进行比较时比头(例如标签)

git difftool --commit=v1.0.0 [-- <filename>*]

注意:由于Git 2.5, git config diff.tool winmerge 就足够了!
请参阅“ git mergetool winmerge

自Git 1.7.11 以来,您拥有选项 - Dir-Diff ,以便为了产生外部DIFF工具,可以在填充两个临时目录后的一次可以比较两个目录层次结构,而不是每个文件对运行一次外部工具的实例。


Git 2.5之前:

使用自定义差异工具配置 difftool 的实际情况:

C:\myGitRepo>git config --global diff.tool winmerge
C:\myGitRepo>git config --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\""
C:\myGitRepo>git config --global difftool.prompt false

使用Winmerge.sh存储在路径的目录部分中:

#!/bin/sh
echo Launching WinMergeU.exe: $1 $2
"C:/Program Files/WinMerge/WinMergeU.exe" -u -e "$1" "$2" -dl "Local" -dr "Remote"

如果您有另一个工具(KDIFF3,P4DIFF,...),创建另一个shell脚本和适当的 fifftool.mydifftool.cmd config指令。
然后,您可以使用 diff.tool config轻松切换工具。

您还拥有此添加其他详细信息。
(或
此问题对于 winmergeu 选项)

此设置的兴趣是 winmerge.sh 脚本:您可以将其自定义以考虑特殊情况。

请参阅例如 David Marble with-visual-diff-program/4116806#4116806“>下面的答案 有关:

  • How-do-ii-view-git-diff-output -

kem Mason a href =“ https://stackoverflow.com/questions/255202/how-do-i-i-i-view-git-diff-diff-dput-with-with-visual-visual-diff-program/4881489#4881489>您还可以使用 - Extcmd option :避免任何包装器:

--extcmd=<command>

指定用于查看差异的自定义命令。 git-difftool 忽略已配置的默认值并运行 $命令$ local $ remote 指定此选项时。

例如,这就是 gitk 能够运行/使用任何 diff 工具

To complete my previous "diff.external" config answer above:

As mentioned by Jakub, Git1.6.3 introduced git difftool, originally proposed in September 2008:

USAGE='[--tool=tool] [--commit=ref] [--start=ref --end=ref] [--no-prompt] [file to merge]'
(See --extcmd in the last part of this answer)

$LOCAL contains the contents of the file from the starting revision and $REMOTE contains the contents of the file in the ending revision.
$BASE contains the contents of the file in the wor

It's basically git-mergetool modified to operate on the git index/worktree.

The usual use case for this script is when you have either staged or unstaged changes and you'd like to see the changes in a side-by-side diff viewer (e.g. xxdiff, tkdiff, etc).

git difftool [<filename>*]

Another use case is when you'd like to see the same information but are comparing arbitrary commits (this is the part where the revarg parsing could be better)

git difftool --start=HEAD^ --end=HEAD [-- <filename>*]

The last use case is when you'd like to compare your current worktree to something other than HEAD (e.g. a tag)

git difftool --commit=v1.0.0 [-- <filename>*]

Note: since Git 2.5, git config diff.tool winmerge is enough!
See "git mergetool winmerge"

And since Git 1.7.11, you have the option --dir-diff, in order to to spawn external diff tools that can compare two directory hierarchies at a time after populating two temporary directories, instead of running an instance of the external tool once per a file pair.


Before Git 2.5:

Practical case for configuring difftool with your custom diff tool:

C:\myGitRepo>git config --global diff.tool winmerge
C:\myGitRepo>git config --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\""
C:\myGitRepo>git config --global difftool.prompt false

With winmerge.sh stored in a directory part of your PATH:

#!/bin/sh
echo Launching WinMergeU.exe: $1 $2
"C:/Program Files/WinMerge/WinMergeU.exe" -u -e "$1" "$2" -dl "Local" -dr "Remote"

If you have another tool (kdiff3, P4Diff, ...), create another shell script, and the appropriate difftool.myDiffTool.cmd config directive.
Then you can easily switch tools with the diff.tool config.

You have also this blog entry by Dave to add other details.
(Or this question for the winmergeu options)

The interest with this setting is the winmerge.shscript: you can customize it to take into account special cases.

See for instance David Marble's answer below for an example which deals with:

  • new files in either origin or destination
  • removed files in either origin or destination

As Kem Mason mentions in his answer, you can also avoid any wrapper by using the --extcmd option:

--extcmd=<command>

Specify a custom command for viewing diffs. git-difftool ignores the configured defaults and runs $command $LOCAL $REMOTE when this option is specified.

For instance, this is how gitk is able to run/use any diff tool.

俯瞰星空 2025-02-10 08:34:02

尝试此解决方案:

$ meld my_project_using_git

meld 了解Git并围绕最近的更改提供导航。

Try this solution:

$ meld my_project_using_git

Meld understands Git and provides navigating around the recent changes.

鹿! 2025-02-10 08:34:02

使用新的git difftool ,它与将其添加到您的 .gitConfig file:

[diff]
    tool = any-name
[difftool "any-name"]
    cmd = "\"C:/path/to/my/ext/diff.exe\" \"$LOCAL\" \"$REMOTE\""

可选的是:

[difftool]
    prompt = false

也请查看 diffall ,我写的一个简单的脚本是为了扩展串行打开每个人的烦人(IMO)默认的差异行为。

Windows上的Global .gitConfig在%userProfile%\。gitconfig

With new git difftool, its as simple as adding this to your .gitconfig file:

[diff]
    tool = any-name
[difftool "any-name"]
    cmd = "\"C:/path/to/my/ext/diff.exe\" \"$LOCAL\" \"$REMOTE\""

Optionally, also add:

[difftool]
    prompt = false

Also check out diffall, a simple script I wrote to extend the annoying (IMO) default diff behaviour of opening each in serial.

Global .gitconfig on Windows is in %USERPROFILE%\.gitconfig

一萌ing 2025-02-10 08:34:02

由于git版本1.6.3有“ git difftool ”,您可以配置以使用自己喜欢的图形差异工具。

目前受支持(在编写此答案时)开箱即用的是 kdiff3 ,, kompare tkdiff meld xxdiff ,Emerge,vimdiff,gvimdiff,ecmerge, opendiff ;如果您要使用的工具不在此列表中,则可以始终使用' difftool。

“ git difftool”接受与“ git diff”相同的选项。

Since Git version 1.6.3 there is "git difftool" which you can configure to use your favorite graphical diff tool.

Currently supported (at the time of writing this answer) out-of-the-box are KDiff3, Kompare, tkdiff, Meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge, Diffuse and opendiff; if the tool you want to use isn't on this list, you can always use 'difftool.<tool>.cmd' configuration option.

"git difftool" accepts the same options as "git diff".

他不在意 2025-02-10 08:34:02

我有一个补充。我喜欢定期使用不支持默认工具之一(例如万花筒)的diff

git difftool -t

应用 git_external_diff 变量不是一个选项。

您可以将任意 diff 应用程序用作与此命令的一次性:

git difftool --extcmd=/usr/bin/ksdiff

它只是将2个文件传递给您指定的命令,因此您可能也不需要包装器。

I have one addition to this. I like to regularly use a diff app that isn't supported as one of the default tools (e.g. kaleidoscope), via

git difftool -t

I also like to have the default diff just be the regular command line, so setting the GIT_EXTERNAL_DIFF variable isn't an option.

You can use an arbitrary diff app as a one-off with this command:

git difftool --extcmd=/usr/bin/ksdiff

It just passes the 2 files to the command you specify, so you probably don't need a wrapper either.

淡看悲欢离合 2025-02-10 08:34:02

建立 VONC的答案要处理文件删除和添加,请使用以下命令和脚本:

git config --global diff.tool winmerge
git config --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\" \"$BASE\""
git config --global difftool.prompt false

这与将其放在您的全局文件 .gitConfig 中相同:

[diff]
    tool = winmerge
[difftool "winmerge"]
    cmd = winmerge.bat "$LOCAL" "$REMOTE" "$BASE"
[difftool]
    prompt = false

然后将以下内容放入文件 winmerge.sh 必须在您的道路上:

#!/bin/sh
NULL="/dev/null"
if [ "$2" = "$NULL" ] ; then
    echo "removed: $3"
elif [ "$1" = "$NULL" ] ; then
    echo "added: $3"
else
    echo "changed: $3"
    "C:/Program Files (x86)/WinMerge/WinMergeU.exe" -e -ub -dl "Base" -dr "Mine" "$1" "$2"
fi

Building on VonC's answer to deal with file removals and additions, use the following commands and scripts:

git config --global diff.tool winmerge
git config --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\" \"$BASE\""
git config --global difftool.prompt false

Which is the same as putting this in your global file .gitconfig:

[diff]
    tool = winmerge
[difftool "winmerge"]
    cmd = winmerge.bat "$LOCAL" "$REMOTE" "$BASE"
[difftool]
    prompt = false

Then put the following in file winmerge.sh which must be on your path:

#!/bin/sh
NULL="/dev/null"
if [ "$2" = "$NULL" ] ; then
    echo "removed: $3"
elif [ "$1" = "$NULL" ] ; then
    echo "added: $3"
else
    echo "changed: $3"
    "C:/Program Files (x86)/WinMerge/WinMergeU.exe" -e -ub -dl "Base" -dr "Mine" "$1" "$2"
fi
深海少女心 2025-02-10 08:34:02

Windows/ msys

阅读答案后的 git仅更改一个文件。

  1. 创建一个批处理文件以调用您的diff程序,并使用参数2和5。此文件必须在您的路径中。 (如果您不知道那在哪里,请将其放入 c:\ windows 。)例如“ gitdiff.bat”。我的是:

      @echo关闭
    rem这是gitdiff.bat
    “ c:\ program文件\ winmerge \ winmergeu.exe”%2%5
     
  2. 设置环境变量以指向批处理文件。例如: git_external_diff = gitdiff.bat 。或通过powershell键入 git config -global diff.external gitdiff.bat

    不使用引号或指定任何路径信息很重要,否则它行不通。这就是为什么 gitdiff.bat 必须在您的路径中。

现在,当您键入“ git diff”时,它将调用您的外部diff查看器。

Solution for Windows/MSYS Git

After reading the answers, I discovered a simpler way that involves changing only one file.

  1. Create a batch file to invoke your diff program, with argument 2 and 5. This file must be somewhere in your path. (If you don't know where that is, put it in C:\windows.) Call it, for example, "gitdiff.bat". Mine is:

    @echo off
    REM This is gitdiff.bat
    "C:\Program Files\WinMerge\WinMergeU.exe" %2 %5
    
  2. Set the environment variable to point to your batch file. For example:GIT_EXTERNAL_DIFF=gitdiff.bat. Or through PowerShell by typing git config --global diff.external gitdiff.bat.

    It is important to not use quotes, or specify any path information, otherwise it won't work. That's why gitdiff.bat must be in your path.

Now when you type "git diff", it will invoke your external diff viewer.

请你别敷衍 2025-02-10 08:34:02

如果您是通过 cygwin ,您可能需要使用 cygpath

$ git config difftool.bc3.cmd "git-diff-bcomp-wrapper.sh \$LOCAL \$REMOTE"
$ cat git-diff-bcomp-wrapper.sh
#!/bin/sh
"c:/Program Files (x86)/Beyond Compare 3/BComp.exe" `cygpath -w $1` `cygpath -w $2`

If you're doing this through Cygwin, you may need to use cygpath:

$ git config difftool.bc3.cmd "git-diff-bcomp-wrapper.sh \$LOCAL \$REMOTE"
$ cat git-diff-bcomp-wrapper.sh
#!/bin/sh
"c:/Program Files (x86)/Beyond Compare 3/BComp.exe" `cygpath -w $1` `cygpath -w $2`
围归者 2025-02-10 08:34:02

查看了其他一些外部差异工具后,我发现Intellij Idea(和Android Studio)中的 diff 视图对我来说是最好的。

步骤1-如果要使用Intellij Ideas作为diff工具,则应从命令行中运行Intellij Ideas

,请首先设置Intellij Idey以按照指令从命令行运行

​或unix:

  1. 确保Intellij Idea正在运行。
  2. 在主菜单上,选择工具|创建命令行启动器。对话框创建启动器脚本打开,并带有启动器脚本的建议路径和名称。您可以接受默认值或指定自己的路径。
    注意它,因为稍后您需要。
    在Intellij Idea之外,将启动器脚本的路径和名称添加到您的路径中。

在Windows上:

  1. 在路径系统环境变量中指定Intellij Idea的位置。在这种情况下,您将能够从任何目录中调用Intellij Ideas可执行文件和其他Intellij Ideas命令。

步骤2-配置git以按照

此博客文章

bash

export INTELLIJ_HOME /Applications/IntelliJ\ IDEA\ CE.app/Contents/MacOS
PATH=$IDEA_HOME $PATH

fish

set INTELLIJ_HOME /Applications/IntelliJ\ IDEA\ CE.app/Contents/MacOS
set PATH $INTELLIJ_HOME $PATH

现在将以下内容添加到您的git配置:

[merge]
   tool = intellij
[mergetool "intellij"]
   cmd = idea merge $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE") $(cd $(dirname "$BASE") && pwd)/$(basename "$BASE") $(cd $(dirname "$MERGED") && pwd)/$(basename "$MERGED")
   trustExitCode = true
[diff]
   tool = intellij
[difftool "intellij"]
   cmd = idea diff $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE")

您可以使用尝试一下git difftool git difftool头〜1

After looking at some other external diff tools, I found that the diff view in IntelliJ IDEA (and Android Studio) is the best one for me.

Step 1 - setup IntelliJ IDEA to be run from the command line

If you want to use IntelliJ IDEA as your diff tool you should first setup IntelliJ IDEA to be run from the command line following the instructions here:

On macOS or UNIX:

  1. Make sure IntelliJ IDEA is running.
  2. On the main menu, choose Tools | Create Command-line Launcher. The dialog box Create Launcher Script opens, with the suggested path and name of the launcher script. You can accept default, or specify your own path.
    Make notice of it, as you'll need it later.
    Outside of IntelliJ IDEA, add the path and name of the launcher script to your path.

On Windows:

  1. Specify the location of the IntelliJ IDEA executable in the Path system environment variable. In this case, you will be able to invoke the IntelliJ IDEA executable and other IntelliJ IDEA commands from any directory.

Step 2 - configure git to use IntelliJ IDEA as the difftool

Following the instructions on this blog post:

Bash

export INTELLIJ_HOME /Applications/IntelliJ\ IDEA\ CE.app/Contents/MacOS
PATH=$IDEA_HOME $PATH

Fish

set INTELLIJ_HOME /Applications/IntelliJ\ IDEA\ CE.app/Contents/MacOS
set PATH $INTELLIJ_HOME $PATH

Now add the following to your git config:

[merge]
   tool = intellij
[mergetool "intellij"]
   cmd = idea merge $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE") $(cd $(dirname "$BASE") && pwd)/$(basename "$BASE") $(cd $(dirname "$MERGED") && pwd)/$(basename "$MERGED")
   trustExitCode = true
[diff]
   tool = intellij
[difftool "intellij"]
   cmd = idea diff $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE")

You can try it out with git difftool or git difftool HEAD~1

失眠症患者 2025-02-10 08:34:02

以前的出色答案的简短摘要:

git difftool --tool-help
git config --global diff.tool <chosen tool>
git config --global --add difftool.prompt false

然后通过键入(也可以选择指定文件名)使用它:

git difftool

A short summary of the previous great answers:

git difftool --tool-help
git config --global diff.tool <chosen tool>
git config --global --add difftool.prompt false

Then use it by typing (optionally specifying the file name as well):

git difftool
千笙结 2025-02-10 08:34:02

这对我在Windows&nbsp; 7上有用。无需中介 sh scripts

的内容。 gitconfig:

    [diff]
      tool = kdiff3

    [difftool]
       prompt = false

    [difftool "kdiff3"]
      path = C:/Program Files (x86)/KDiff3/kdiff3.exe
      cmd = "$LOCAL" "$REMOTE"

This works for me on Windows 7. There isn't any need for intermediary sh scripts

Contents of .gitconfig:

    [diff]
      tool = kdiff3

    [difftool]
       prompt = false

    [difftool "kdiff3"]
      path = C:/Program Files (x86)/KDiff3/kdiff3.exe
      cmd = "$LOCAL" "$REMOTE"
北笙凉宸 2025-02-10 08:34:02

安装 meld

 # apt-get install meld

然后选择它作为difftool:

 $ git config --global diff.tool meld

如果您想在控制台中运行它,请键入:

 $ git difftool

如果您需要:如果需要要使用图形模式,请键入:

 $ git mergetool

输出将是:

 'git mergetool' will now attempt to use one of the following tools:
 meld opendiff kdiff3 tkdiff xxdiff tortoisemerge gvimdiff diffuse
 diffmerge ecmerge p4merge araxis bc3 codecompare emerge vimdiff
 Merging:
 www/css/style.css
 www/js/controllers.js

 Normal merge conflict for 'www/css/style.css':
   {local}: modified file
   {remote}: modified file
 Hit return to start merge resolution tool (meld):

因此,只需按 enter 使用meld(默认)。这将打开图形模式。使魔术保存并按下解决合并的问题。就这样。

Install Meld:

 # apt-get install meld

Then choose that as the difftool:

 $ git config --global diff.tool meld

If you want to run it in the console, type:

 $ git difftool

If you want to use graphic mode, type:

 $ git mergetool

And the output would be:

 'git mergetool' will now attempt to use one of the following tools:
 meld opendiff kdiff3 tkdiff xxdiff tortoisemerge gvimdiff diffuse
 diffmerge ecmerge p4merge araxis bc3 codecompare emerge vimdiff
 Merging:
 www/css/style.css
 www/js/controllers.js

 Normal merge conflict for 'www/css/style.css':
   {local}: modified file
   {remote}: modified file
 Hit return to start merge resolution tool (meld):

So just press Enter to use meld (default). This would open graphic mode. Make the magic save and press that that resolve the merge. That's all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文