gpt4 book ai didi

git - 为什么 `git diff` 不调用外部差异工具?

转载 作者:IT王子 更新时间:2023-10-29 01:06:22 24 4
gpt4 key购买 nike

在我的仓库中,如果我输入

$ git diff some-file

$ git difftool some-file

我得到终端内差异显示。我认为这不应该发生,因为我已经设置了一个外部差异工具,如 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

diff.external 行中引用的 git-diff 文件如下所示

#!/bin/bash

meld $2 $5

为什么 git diff 不调用 meld?

如果我进行设置以便 git config -l 具有以下行,我会得到相同的行为:

diff.tool = meld

diff.external = usr/bin/meld

注意:我机器上的其他仓库没有这个问题。

相关但不等同的 SO 问题:

  1. What is the difference between git diff and git difftool?
  2. Cannot make git diff use diff.external for external diff tool

最佳答案

I get the in-terminal diff display. I this should not happen, because I have set up an external diff tool

是的,它应该:diff.external “终端内差异显示”。

(来自 git config man page)

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 the GIT_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.

question you link解释了为什么 meld 不能扮演“外部差异”的角色。

Viewing a diff visually with another tool完成:

git difftool --dir-diff shaOfHisCheckIn^!
git difftool --tool=meld --dir-diff shaOfHisCheckIn^!
git difftool -t meld -d shaOfHisCheckIn^!

meld 可以在 Windows 上配置为 difftool:参见“Git Diff and Meld on Windows”。


如果你想为 git diff 配置 meld,你可以 (on Ubuntu) use the diff.external, but with a wrapper script :

create a file called git-diff.sh, using the following content:

#!/bin/bash
meld "$2" "$5" > /dev/null 2>&1

Save this to a location such as /usr/local/bin, giving it executable rights:

$ 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:

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


will the comments 中的注释:

That wrapper script works great on my Ubuntu system.

I did discover a gottcha: I detached the meld command as so:

meld "$2" "$5" > /dev/null 2>&1 &

Kept failing to open the $2 file (tempory file).
What happens? Well, git removes $2 once the wrapper exits.
My suggestion, if you wish to detach copy the tmp file, invoke meld and remove tmp file yourself.

将求婚the gist gist-meld.bash ,使用 meld --newtab,作为 seen here :

#!/bin/bash
# * expects meld to be on your default PATH
#
function detach_meld()
{
local f1="/tmp/mld1-$(basename "$1")"
local f2="/tmp/mld2-$(basename "$2")"

## echo "f1 = ${f1}"
## echo "f2 = ${f2}"

cp "$1" "${f1}"
cp "$2" "${f2}"
#
(meld --newtab "${f1}" "${f2}" ; rm "${f1}" "${f2}" ) > /dev/null 2>&1 &
}

detach_meld "$2" "$5"

关于git - 为什么 `git diff` 不调用外部差异工具?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24966271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com