为什么`git diff`不调用外部差异工具?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24966271/
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
Why doesn't `git diff` invoke external diff tool?
提问by DanielSank
In my repository, if I type
在我的存储库中,如果我输入
$ git diff some-file
or
或者
$ git difftool some-file
I get the in-terminal diff display. I think this should not happen, because I have set up an external diff tool, as shown by the output of git config -l
:
我得到终端内差异显示。我认为这不应该发生,因为我已经设置了一个外部差异工具,如输出所示git config -l
:
$ git config -l
user.name=blah blah
user.email=blah blah
http.sslverify=true
diff.external=/home/daniel/bin/git-diff <--This is the important line
push.default=simple
core.filemode=false
core.editor=gedit
alias.tree=log --all --graph --decorate=short --color --format=format:'%C(bold blue)%h%C(reset) %C(auto)%d%C(reset)
%C(black)[%cr]%C(reset) %x09%C(black)%an: %s %C(reset)'
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
remote.origin.url=https://daniel@skynet/git/pyle.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.daniel.remote=origin
branch.daniel.merge=refs/heads/daniel
The git-diff file referenced in the diff.external
line looks like this
该diff.external
行中引用的 git-diff 文件如下所示
#!/bin/bash
meld
Why doesn't git diff
invoke meld?
为什么不git diff
调用meld?
I get the same behaviour if I set things up so that git config -l
has the following line:
如果我设置git config -l
了以下内容,我会得到相同的行为:
diff.tool = meld
or
或者
diff.external = usr/bin/meld
Note: Other repositories on my machine don't have this problem.
注意:我机器上的其他存储库没有这个问题。
Related, but not equivalent, SO questions:
相关但不等价的 SO 问题:
采纳答案by VonC
I get the in-terminal diff display. I this should not happen, because I have set up an external diff tool
我得到终端内差异显示。我这不应该发生,因为我已经设置了一个外部差异工具
Yes, it should: diff.external isfor "in-terminal diff display".
是的,它应该:diff.external就是对“在终端显示DIFF”。
(from git config
man page)
(来自git config
手册页)
diff.external
If this config variable is set, diff generation is not performed using the internal diff machinery, but using the given command.
Can be overridden with theGIT_EXTERNAL_DIFF
environment variable.
The command is called with parameters as described under "git Diffs" in git(1). Note: if you want to use an external diff program only on a subset of your files, you might want to use gitattributes(5)instead.
如果设置了此配置变量,则不会使用内部差异机制执行差异生成,而是使用给定的命令。
可以用GIT_EXTERNAL_DIFF
环境变量覆盖。
该命令使用 git(1) 中“git Diffs”中所述的参数调用。注意:如果您只想在文件的一个子集上使用外部 diff 程序,您可能需要使用gitattributes(5)代替。
The question you linkexplains why meld
wouldn't be able to play the role of an "external diff".
您链接的问题解释了为什么meld
不能扮演“外部差异”的角色。
Viewing a diff visually with another toolis done with:
使用另一个工具直观地查看差异是通过以下方式完成的:
git difftool --dir-diff shaOfHisCheckIn^!
git difftool --tool=meld --dir-diff shaOfHisCheckIn^!
git difftool -t meld -d shaOfHisCheckIn^!
meld
can be configured on Windows as a difftool: see "Git Diff and Meld on Windows".
meld
可以在 Windows 上配置为 difftool:请参阅“ Windows 上的 Git Diff 和 Meld”。
If you wanted to configure meld for git diff, you could (on Ubuntu) use the diff.external
, but with a wrapper script:
如果您想为 git diff 配置 meld,您可以(在 Ubuntu 上)使用diff.external
, 但使用包装脚本:
create a file called
git-diff.sh
, using the following content:
git-diff.sh
使用以下内容创建一个名为 的文件:
#!/bin/bash
meld "" "" > /dev/null 2>&1
Save this to a location such as
/usr/local/bin
, giving it executable rights:
将其保存到一个位置,例如
/usr/local/bin
,赋予其可执行权限:
$ sudo mv git-diff.sh /usr/local/bin/
$ sudo chmod +x /usr/local/bin/git-diff.sh
The final step is to open your
$HOME/.gitconfig
file and add the following few lines:
最后一步是打开您的
$HOME/.gitconfig
文件并添加以下几行:
[diff]
external = /usr/local/bin/git-diff.sh
The next time you type git diff in a Git project with changes, Meld will be launched showing you a split-pane diff viewer.
Note that you are required to close the open instance of meld before the next diff viewer is opened.
下次您在带有更改的 Git 项目中键入 git diff 时,将启动 Meld 向您显示一个拆分窗格差异查看器。
请注意,在打开下一个差异查看器之前,您需要关闭打开的 meld 实例。
回答by Trebor Rude
It seems that git diff
(at least, as of git
version 1.7.12.4
) will not run anything other than the internal, console-only diff on a file which is in the "both modified" state. git mergetool
works on such files, though.
似乎git diff
(至少,从git
version 开始1.7.12.4
)不会在处于“两者修改”状态的文件上运行除内部仅控制台差异之外的任何内容。git mergetool
不过,适用于此类文件。
回答by Lodewyk
I usually just want to check whether the changes I'm about to check in are correct. So I followed the procedure suggested here(similar to the one noted by VonC). However, running git difftool
still didn't open meld. I then created an alias:
我通常只想检查我将要检查的更改是否正确。所以我遵循了这里建议的程序(类似于 VonC 指出的程序)。然而,跑步git difftool
仍然没有打开融合。然后我创建了一个别名:
alias git-diff='git difftool $(git rev-parse HEAD)'
Save this in your .bashrc or .zshrc or the corresponding config for your shell. This essentially compares the state of the branch with the previous commit on the branch.
将其保存在您的 .bashrc 或 .zshrc 或您的 shell 的相应配置中。这实质上是将分支的状态与分支上的先前提交进行比较。
Do a git-diff
to see changes on a file per file basis or git-diff --dir
to see all changes in a directory view.
执行 agit-diff
以查看每个文件的文件git-diff --dir
更改或查看目录视图中的所有更改。