当我执行“git diff”时,如何获得并排差异?

发布于 2024-12-08 07:18:43 字数 125 浏览 1 评论 0原文

当我输入 git diff 时,我希望看到并排的差异,就像使用 diff -y 一样,或者希望在交互式差异中显示差异像kdiff3这样的工具。这怎么能做到呢?

When I type git diff, I'd like to see a side-by-side diff, like with diff -y, or like to display the diff in an interactive diff tool like kdiff3. How can this be done?

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

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

发布评论

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

评论(19

¢好甜 2024-12-15 07:18:43

尝试 git difftool

使用 git difftool 而不是 git diff。你永远不会回去。

更新以添加示例用法:

这是另一个有关 git difftool 的 stackoverflow 的链接:如何使用我的首选 diff 工具/查看器查看“git diff”输出?

对于较新版本的 git , 这difftool 命令支持许多开箱即用的外部比较工具。例如,vimdiff 是自动支持的,可以通过以下方式从命令行打开:

cd /path/to/git/repo
git difftool --tool=vimdiff

其他支持的外部 diff 工具通过 git difftool --tool-help 列出,这是一个示例输出:

'git difftool --tool=<tool>' may be set to one of the following:
        araxis
        kompare
        vimdiff
        vimdiff2

The following tools are valid, but not currently available:
        bc3
        codecompare
        deltawalker
        diffuse
        ecmerge
        emerge
        gvimdiff
        gvimdiff2
        kdiff3
        meld
        opendiff
        tkdiff
        xxdiff

Try git difftool

Use git difftool instead of git diff. You'll never go back.

UPDATE to add an example usage:

Here is a link to another stackoverflow that talks about git difftool: How do I view 'git diff' output with my preferred diff tool/ viewer?

For newer versions of git, the difftool command supports many external diff tools out-of-the-box. For example vimdiff is auto supported and can be opened from the command line by:

cd /path/to/git/repo
git difftool --tool=vimdiff

Other supported external diff tools are listed via git difftool --tool-help here is an example output:

'git difftool --tool=<tool>' may be set to one of the following:
        araxis
        kompare
        vimdiff
        vimdiff2

The following tools are valid, but not currently available:
        bc3
        codecompare
        deltawalker
        diffuse
        ecmerge
        emerge
        gvimdiff
        gvimdiff2
        kdiff3
        meld
        opendiff
        tkdiff
        xxdiff
揽月 2024-12-15 07:18:43

尽管 Git 有 diff 的内部实现,但您可以设置一个外部工具。

有两种不同的方法可以指定外部 diff 工具:

  1. 设置 GIT_EXTERNAL_DIFF 和 GIT_DIFF_OPTS 环境变量。
  2. 通过 git config 配置外部 diff 工具

ymattw 的答案也非常简洁,使用 ydiff

另请参阅:

进行 git diff 时, Git 检查上述环境变量的设置及其 .gitconfig 文件。

默认情况下,Git 将以下七个参数传递给 diff 程序:

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

您通常只需要旧文件和新文件参数。当然,大多数 diff 工具只接受两个文件名作为参数。这意味着您需要编写一个小的包装脚本,它接受 Git 提供给脚本的参数,并将它们传递给您选择的外部 git 程序。

假设您将包装脚本放在 ~/scripts/my_diff.sh 下:

#!/bin/bash
# un-comment one diff tool you'd like to use

# use standard diff command with options: (2023)
/usr/bin/diff -y "$2" "$5" 

# side-by-side diff with custom options:
# /usr/bin/sdiff -w200 -l "$2" "$5" 

# using kdiff3 as the side-by-side diff:
# /usr/bin/kdiff3 "$2" "$5"

# using Meld 
/usr/bin/meld "$2" "$5"

# using VIM
# /usr/bin/vim -d "$2" "$5"

然后您需要使该脚本可执行:

chmod a+x ~/scripts/my_diff.sh

然后您需要告诉 Git 如何以及在哪里找到您的自定义 diff 包装脚本。
您有三种选择:(我更喜欢编辑 .gitconfig 文件)

  1. 使用 GIT_EXTERNAL_DIFFGIT_DIFF_OPTS

    例如,您可以在 .bashrc 或 .bash_profile 文件中设置:

     GIT_EXTERNAL_DIFF=$HOME/scripts/my_diff.sh
     导出 GIT_EXTERNAL_DIFF
    
  2. 例如,您可以设置:

    使用 git config

    使用“git config”来定义包装脚本的位置:

     git config --global diff.external ~/scripts/my_diff.sh
    
  3. 编辑您的~/.gitconfig 文件

    您可以编辑 ~/.gitconfig 文件来添加以下行:

    <前><代码> [差异]
    外部 = ~/scripts/my_diff.sh

注意:

与安装自定义 diff 工具类似,您还可以安装自定义合并工具,它可以是一个可视化合并工具,可以更好地帮助可视化合并。 (请参阅 progit.org 页面)

请参阅:http://fredpalma.com /518/visual-diff-and-merge-tool/https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration

Although Git has an internal implementation of diff, you can set up an external tool instead.

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

  1. setting the GIT_EXTERNAL_DIFF and the GIT_DIFF_OPTS environment variables.
  2. configuring the external diff tool via git config

ymattw's answer is also pretty neat, using ydiff

See also:

When doing a git diff, Git checks both the settings of above environment variables and its .gitconfig file.

By default, Git passes the following seven arguments to the diff program:

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

You typically only need the old-file and new-file parameters. Of course most diff tools only take two file names as an argument. This means that you need to write a small wrapper-script, which takes the arguments which Git provides to the script, and hands them on to the external git program of your choice.

Let's say you put your wrapper-script under ~/scripts/my_diff.sh:

#!/bin/bash
# un-comment one diff tool you'd like to use

# use standard diff command with options: (2023)
/usr/bin/diff -y "$2" "$5" 

# side-by-side diff with custom options:
# /usr/bin/sdiff -w200 -l "$2" "$5" 

# using kdiff3 as the side-by-side diff:
# /usr/bin/kdiff3 "$2" "$5"

# using Meld 
/usr/bin/meld "$2" "$5"

# using VIM
# /usr/bin/vim -d "$2" "$5"

you then need to make that script executable:

chmod a+x ~/scripts/my_diff.sh

you then need to tell Git how and where to find your custom diff wrapper script.
You have three choices how to do that: (I prefer editing the .gitconfig file)

  1. Using GIT_EXTERNAL_DIFF, GIT_DIFF_OPTS

    e.g. in your .bashrc or .bash_profile file you can set:

     GIT_EXTERNAL_DIFF=$HOME/scripts/my_diff.sh
     export GIT_EXTERNAL_DIFF
    
  2. Using git config

    use "git config" to define where your wrapper script can be found:

     git config --global diff.external ~/scripts/my_diff.sh
    
  3. Editing your ~/.gitconfig file

    you can edit your ~/.gitconfig file to add these lines:

     [diff]
       external = ~/scripts/my_diff.sh
    

Note:

Similarly to installing your custom diff tool, you can also install a custom merge-tool, which could be a visual merging tool to better help visualizing the merge. (see the progit.org page)

See: http://fredpalma.com/518/visual-diff-and-merge-tool/ and https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration

他是夢罘是命 2024-12-15 07:18:43

您还可以尝试 git diff --word-diff 。
它并不完全是并排的,但在某种程度上更好,所以您可能更喜欢它而不是您实际的并排需求。

You can also try git diff --word-diff.
It's not exactly side-by-side, but somehow better, so you might prefer it to your actual side-by-side need.

木緿 2024-12-15 07:18:43

ydiff

以前称为cdiff,该工具可以显示并排增量、和彩色差异。

不要执行 git diff,而是执行以下操作:

ydiff -s -w0

这将为每个有差异的文件以并排显示模式启动 ydiff。

安装:

python3 -m pip install --user ydiff

- 或 -

brew install ydiff

对于 git log,您可以使用:

ydiff -ls -w0

-w0 自动检测您的终端宽度。有关详细信息和演示,请参阅 ydiff GitHub 存储库页面

在 Git 2.18.0、ydiff 1.1 中测试。

ydiff

Formerly called cdiff, this tool can display side by side, incremental, and colorful diff.

Instead of doing git diff, do:

ydiff -s -w0

This will launch ydiff in side-by-side display mode for each of the files with differences.

Install with:

python3 -m pip install --user ydiff

-or-

brew install ydiff

For git log, you can use:

ydiff -ls -w0

-w0 auto-detects your terminal width. See the ydiff GitHub repository page for detail and demo.

Tested in Git 2.18.0, ydiff 1.1.

半步萧音过轻尘 2024-12-15 07:18:43

您可以使用 sdiff 进行并排 diff,如下所示:

$ git difftool -y -x sdiff  HEAD^ | less

其中 HEAD^ 是一个示例,您应该将其替换为您想要的任何内容来区别。

我在这里找到了这个解决方案还有其他一些建议。然而,这个答案简洁明了地回答了OP的问题。

有关参数的说明,请参阅 man git-difftool


考虑到板上的注释,您可以通过编写以下可执行脚本来创建一个方便的 git sdiff 命令:

#!/bin/sh
git difftool -y -x "sdiff -w $(tput cols)" "${@}" | less

将其另存为 /usr/bin/git-sdiffchmod +x 它。然后你就可以这样做:

$ git sdiff HEAD^

额外提示

正如评论中所建议的,你可以使用icdiff来执行sdiff对彩色输出所做的操作:

$ more /usr/bin/git-sdiff
#!/bin/sh
git difftool -y -x "icdiff --cols $(tput cols)" "${@}" | less --raw-control-chars

You can do a side-by-side diff using sdiff as follows:

$ git difftool -y -x sdiff  HEAD^ | less

where HEAD^ is an example that you should replace with whatever you want to diff against.

I found this solution here where there are a couple of other suggestions also. However, this one answer's the OP's question succinctly and clearly.

See the man git-difftool for an explanation of the arguments.


Taking the comments on board, you can create a handy git sdiff command by writing the following executable script:

#!/bin/sh
git difftool -y -x "sdiff -w $(tput cols)" "${@}" | less

Save it as /usr/bin/git-sdiff and chmod +x it. Then you'll be able to do this:

$ git sdiff HEAD^

Extra Tip

As suggested in comments you can use icdiff to do what sdiff does with colored output:

$ more /usr/bin/git-sdiff
#!/bin/sh
git difftool -y -x "icdiff --cols $(tput cols)" "${@}" | less --raw-control-chars
一城柳絮吹成雪 2024-12-15 07:18:43

我最近实现了一个工具,它正是这样做的: https://github.com/banga/git-split -diffs

以下是如何使用它:

npm install -g git-split-diffs

git config --global core.pager "git-split-diffs --color | less -RFX"

这是它在终端中的外观(使用默认主题):

Preview of side by side diffs

如您所见,它还支持语法突出显示和突出显示行内更改的单词

I recently implemented a tool that does exactly this: https://github.com/banga/git-split-diffs

Here's how to use it:

npm install -g git-split-diffs

git config --global core.pager "git-split-diffs --color | less -RFX"

And this is how it looks in your terminal (with the default theme):

Preview of side by side diffs

As you can see, it also supports syntax highlighting and highlighting changed words within lines

我乃一代侩神 2024-12-15 07:18:43

对于 unix,仅结合 git 和内置 diff

git show HEAD:path/to/file | diff -y - path/to/file

当然,您可以将 HEAD 替换为任何其他 git 引用,并且您可能想要添加类似 的内容>-W 170 到 diff 命令。

这假设您只是将目录内容与过去的提交进行比较。两次提交之间的比较更为复杂。如果您的 shell 是 bash,您可以使用“进程替换”:

diff -y -W 170 <(git show REF1:path/to/file) <(git show REF2:path/to/file)

其中 REF1 和 REF2 是 git 引用 – 标签、分支或哈希。

For unix, combining just git and the built-in diff:

git show HEAD:path/to/file | diff -y - path/to/file

Of course, you can replace HEAD with any other git reference, and you probably want to add something like -W 170 to the diff command.

This assumes that you are just comparing your directory contents with a past commit. Comparing between two commits is more complex. If your shell is bash you can use "process substitution":

diff -y -W 170 <(git show REF1:path/to/file) <(git show REF2:path/to/file)

where REF1 and REF2 are git references – tags, branches or hashes.

晒暮凉 2024-12-15 07:18:43
export GIT_EXTERNAL_DIFF='meld $2 $5; echo >/dev/null'

然后简单地:

git diff
export GIT_EXTERNAL_DIFF='meld $2 $5; echo >/dev/null'

then simply:

git diff
街角卖回忆 2024-12-15 07:18:43

如果您希望在不涉及 GitHub 的情况下在浏览器中查看并排差异,您可能会喜欢 git webdiffgit diff 的直接替代品:

$ pip install webdiff
$ git webdiff

tkdiff 等传统 GUI difftools 相比,它具有许多优势,因为它可以为您提供语法突出显示并显示图像差异。

此处了解更多相关信息。

If you'd like to see side-by-side diffs in a browser without involving GitHub, you might enjoy git webdiff, a drop-in replacement for git diff:

$ pip install webdiff
$ git webdiff

This offers a number of advantages over traditional GUI difftools like tkdiff in that it can give you syntax highlighting and show image diffs.

Read more about it here.

山田美奈子 2024-12-15 07:18:43

我使用 colordiff

在 Mac OS X 上,使用 Linux 上的安装方式

$ sudo port install colordiff

可能是 apt get install colordiff 或类似的东西,具体取决于您的发行版。

然后:

$ git difftool --extcmd="colordiff -ydw" HEAD^ HEAD

或者创建一个别名

$ git alias diffy "difftool --extcmd=\"colordiff -ydw\""

然后你就可以使用它了

$ git diffy HEAD^ HEAD

我称之为“diffy”,因为 diff -y 是 unix 中的并排差异。 Colordiff 还添加了更好的颜色。
在选项-ydw中,y用于并排,w用于忽略空格,d 是产生最小的差异(通常你会得到更好的结果作为差异)

I use colordiff.

On Mac OS X, install it with

$ sudo port install colordiff

On Linux is possibly apt get install colordiff or something like that, depending on your distro.

Then:

$ git difftool --extcmd="colordiff -ydw" HEAD^ HEAD

Or create an alias

$ git alias diffy "difftool --extcmd=\"colordiff -ydw\""

Then you can use it

$ git diffy HEAD^ HEAD

I called it "diffy" because diff -y is the side-by-side diff in unix. Colordiff also adds colors, that are nicer.
In the option -ydw, the y is for the side-by-side, the w is to ignore whitespaces, and the d is to produce the minimal diff (usually you get a better result as diff)

幽蝶幻影 2024-12-15 07:18:43

我个人非常喜欢 icdiff

如果您使用的是带有 HomeBrewMac OS X,只需执行 brew install icdiff 即可。

为了正确获取文件标签以及其他很酷的功能,我在 ~/.gitconfig

[pager]
    difftool = true
[diff]
    tool = icdiff
[difftool "icdiff"]
    cmd = icdiff --head=5000 --highlight --line-numbers -L \"$BASE\" -L \"$REMOTE\" \"$LOCAL\" \"$REMOTE\"

使用了它:git difftool

I personally really like icdiff !

If you're on Mac OS X with HomeBrew, just do brew install icdiff.

To get the file labels correctly, plus other cool features, I have in my ~/.gitconfig:

[pager]
    difftool = true
[diff]
    tool = icdiff
[difftool "icdiff"]
    cmd = icdiff --head=5000 --highlight --line-numbers -L \"$BASE\" -L \"$REMOTE\" \"$LOCAL\" \"$REMOTE\"

And I use it like: git difftool

梦里的微风 2024-12-15 07:18:43

使用 delta

在 gitconfig 文件(通常是 ~/.gitconfig~/.config/git/config)中,

添加:

[core]
  pager = delta --light --side-by-side 

Use delta.

In your gitconfig file (usually ~/.gitconfig or ~/.config/git/config),

add:

[core]
  pager = delta --light --side-by-side 
巨坚强 2024-12-15 07:18:43

当我正在寻找一种使用 git 内置方式来查找差异的快速方法时,出现了这个问题。我的解决方案标准:

  • 快速启动,需要内置选项
  • 可以轻松处理多种格式、xml、不同的编程语言
  • 快速识别我发现的大文本文件中的小代码更改

这个答案在 git 中获取颜色。

为了获得并排差异而不是行差异,我调整了 mb14 的优秀 使用以下参数回答此问题:

$ git diff --word-diff-regex="[A-Za-z0-9. ]|[^[:space:]]"

如果您不喜欢额外的 [- 或 {+ 选项可以使用--word-diff=color

$ git diff --word-diff-regex="[A-Za-z0-9. ]|[^[:space:]]" --word-diff=color

这有助于正确比较 json 和 xml 文本以及 java 代码。

总之,当浏览大文件和小文件时,--word-diff-regex 选项具有有用的可见性和颜色设置,与标准行差异相比,可以获得彩色并排源代码体验。线路变化。

This question showed up when I was searching for a fast way to use git builtin way to locate differences. My solution criteria:

  • Fast startup, needed builtin options
  • Can handle many formats easily, xml, different programming languages
  • Quickly identify small code changes in big textfiles

I found this answer to get color in git.

To get side by side diff instead of line diff I tweaked mb14's excellent answer on this question with the following parameters:

$ git diff --word-diff-regex="[A-Za-z0-9. ]|[^[:space:]]"

If you do not like the extra [- or {+ the option --word-diff=color can be used.

$ git diff --word-diff-regex="[A-Za-z0-9. ]|[^[:space:]]" --word-diff=color

That helped to get proper comparison with both json and xml text and java code.

In summary the --word-diff-regex options has a helpful visibility together with color settings to get a colorized side by side source code experience compared to the standard line diff, when browsing through big files with small line changes.

把梦留给海 2024-12-15 07:18:43

打开Intellij IDEA,在“版本控制”工具窗口中选择单个或多个提交,浏览更改文件,然后双击它们以并排检查每个文件的更改。

使用捆绑的命令行启动器,您可以通过简单的 idea some/path

版本控制视图
diff 视图

Open Intellij IDEA, select a single or multiple commits in the "Version Control" tool window, browse changed files, and double click them to inspect changes side by side for each file.

With the bundled command-line launcher you can bring IDEA up anywhere with a simple idea some/path

version control view
diff view

街角卖回忆 2024-12-15 07:18:43

这是一个方法。如果您通过 less 进行管道传输,则 xterm 宽度将设置为 80,这并不是那么热。但如果您继续执行该命令,例如 COLS=210,您就可以使用扩展的 xterm。

gitdiff()
{
    local width=${COLS:-$(tput cols)}
    GIT_EXTERNAL_DIFF="diff -yW$width \$2 \$5; echo >/dev/null" git diff "$@"
}

Here's an approach. If you pipe through less, the xterm width is set to 80, which ain't so hot. But if you proceed the command with, e.g. COLS=210, you can utilize your expanded xterm.

gitdiff()
{
    local width=${COLS:-$(tput cols)}
    GIT_EXTERNAL_DIFF="diff -yW$width \$2 \$5; echo >/dev/null" git diff "$@"
}
戈亓 2024-12-15 07:18:43

其他几个人已经提到了 cdiff 用于 git 并排比较,但没有人给出它的完整实现。

设置 cdiff:

git clone https://github.com/ymattw/cdiff.git
cd cdiff
ln -s `pwd`/cdiff ~/bin/cdiff
hash -r # refresh your PATH executable in bash (or 'rehash' if you use tcsh)
        # or just create a new terminal

编辑 ~/.gitconfig 插入以下行:

[pager]
        diff = false
        show = false

[diff]
        tool = cdiff
        external = "cdiff -s $2 $5 #"

[difftool "cdiff"]
        cmd = cdiff -s \"$LOCAL\" \"$REMOTE\"

[alias]
        showw = show --ext-dif

cdiff 需要关闭分页器才能与 Diff 一起使用,无论如何它本质上是一个分页器,所以这很好。无论这些设置如何,Difftool 都会起作用。

需要 show 别名,因为 git show 仅通过参数支持外部 diff 工具。

diff 外部命令末尾的“#”很重要。 Git 的 diff 命令将 $@ (所有可用的 diff 变量)附加到 diff 命令,但我们只需要两个文件名。因此,我们用 $2 和 $5 显式地调用这两个,然后将 $@ 隐藏在注释后面,否则会使 sdiff 感到困惑。导致出现如下错误:

fatal: <FILENAME>: no such path in the working tree
Use 'git <command> -- <path>...' to specify paths that do not exist locally.

现在生成并排比较的 Git 命令:

git diff <SHA1> <SHA2> 
git difftool <SHA1> <SHA2>
git showw <SHA>

Cdiff 用法:

'SPACEBAR' - Advances the page of the current file.
'Q'        - Quits current file, thus advancing you to the next file.

现在,您可以通过 git diff 和 difftool 进行并排比较。如果您需要的话,您还可以使用 cdiff python 源代码进行高级用户定制。

Several others already mentioned cdiff for git side-by-side diffing but no one gave a full implementation of it.

Setup cdiff:

git clone https://github.com/ymattw/cdiff.git
cd cdiff
ln -s `pwd`/cdiff ~/bin/cdiff
hash -r # refresh your PATH executable in bash (or 'rehash' if you use tcsh)
        # or just create a new terminal

Edit ~/.gitconfig inserting these lines:

[pager]
        diff = false
        show = false

[diff]
        tool = cdiff
        external = "cdiff -s $2 $5 #"

[difftool "cdiff"]
        cmd = cdiff -s \"$LOCAL\" \"$REMOTE\"

[alias]
        showw = show --ext-dif

The pager off is needed for cdiff to work with Diff, it is essentially a pager anyway so this is fine. Difftool will work regardless of these settings.

The show alias is needed because git show only supports external diff tools via argument.

The '#' at the end of the diff external command is important. Git's diff command appends a $@ (all available diff variables) to the diff command, but we only want the two filenames. So we call out those two explicitly with $2 and $5, and then hide the $@ behind a comment which would otherwise confuse sdiff. Resulting in an error that looks like:

fatal: <FILENAME>: no such path in the working tree
Use 'git <command> -- <path>...' to specify paths that do not exist locally.

Git commands that now produce side-by-side diffing:

git diff <SHA1> <SHA2> 
git difftool <SHA1> <SHA2>
git showw <SHA>

Cdiff usage:

'SPACEBAR' - Advances the page of the current file.
'Q'        - Quits current file, thus advancing you to the next file.

You now have side-by-side diff via git diff and difftool. And you have the cdiff python source code for power user customization should you need it.

慈悲佛祖 2024-12-15 07:18:43

这可能是一个有点有限的解决方案,但是使用系统的 diff 命令无需外部工具即可完成工作:

diff -y  <(git show from-rev:the/file/path) <(git show to-rev:the/file/path)
  • 仅使用 --suppress-common-lines 过滤更改行(如果您的 diff 支持该选项)。
  • 在这种情况下没有颜色,只需通常的 diff 标记
  • 就可以调整列宽 --width=term-width;在 Bash 中可以通过 $COLUMNStput cols 获取宽度。

为了更方便,也可以将其包装到辅助 git 脚本中,例如,如下所示的用法:

git diffy the/file/path --from rev1 --to rev2

This may be a somewhat limited solution, but does the job using the system's diff command without external tools:

diff -y  <(git show from-rev:the/file/path) <(git show to-rev:the/file/path)
  • filter just the change lines use --suppress-common-lines (if your diff supports the option).
  • no colors in this case, just the usual diff markers
  • can tweak the column width --width=term-width; in Bash can get the width as $COLUMNS or tput cols.

This can be wrapped into a helper git-script too for more convenience, for example, usage like this:

git diffy the/file/path --from rev1 --to rev2
南冥有猫 2024-12-15 07:18:43

有多种解决方案:

解决方案1:Meld:

安装meld(在ubuntu中我使用sudo apt install meld)。然后像下面这样配置它。

git config --global diff.tool meld
git config --global difftool.meld.path "$(which meld)"
git config --global difftool.prompt false

git config --global merge.tool meld
git config --global mergetool.meld.path "$(which meld)"

解决方案 2:Delta:

如果您决定使用 cli,请安装 delta 。我使用的配置是:

git config --global core.pager 'delta'
git config --global interactive.diffFilter 'delta --color-only'
git config --global delta.side-by-side true
git config --global delta.line-numbers true
git config --global delta.syntax-theme 'Solarized (dark)'

解决方案3:Melt:

您还可以使用Melt。它的语法突出显示是通过 bat 完成的。这也是一个 cli 工具。

There are multiple solutions:

Solution 1 : Meld :

Install meld (in ubuntu I used sudo apt install meld). Then configure it like bellow.

git config --global diff.tool meld
git config --global difftool.meld.path "$(which meld)"
git config --global difftool.prompt false

git config --global merge.tool meld
git config --global mergetool.meld.path "$(which meld)"

Solution 2 : Delta :

If you decide to use cli, then install delta. The config I use is:

git config --global core.pager 'delta'
git config --global interactive.diffFilter 'delta --color-only'
git config --global delta.side-by-side true
git config --global delta.line-numbers true
git config --global delta.syntax-theme 'Solarized (dark)'

Solution 3 : Melt :

You can also use Melt. It's syntax highlighting is done with bat. This is also a cli tool.

情栀口红 2024-12-15 07:18:43

这个线程中有很多很好的答案。我对这个问题的解决方案是编写一个脚本。

将此命名为“git-scriptname”(并使其可执行并将其放入您的 PATH 中,就像任何脚本一样),您可以通过运行像普通 git 命令一样调用它

$ git scriptname

。实际功能只是最后一行。这是来源:

#!/usr/bin/env zsh
#
#   Show a side-by-side diff of a particular file how it currently exists between:
#       * the file system
#       * in HEAD (latest committed changes)

function usage() {
    cat <<-HERE
    USAGE

    $(basename $1) <file>

    Show a side-by-side diff of a particular file between the current versions:

        * on the file system (latest edited changes)
        * in HEAD (latest committed changes)

HERE
}

if [[ $# = 0 ]]; then
    usage $0
    exit
fi

file=$1
diff -y =(git show HEAD:$file) $file | pygmentize -g | less -R

There are a lot of good answers on this thread. My solution for this issue was to write a script.

Name this 'git-scriptname' (and make it executable and put it in your PATH, like any script), and you can invoke it like a normal git command by running

$ git scriptname

The actual functionality is just the last line. Here's the source:

#!/usr/bin/env zsh
#
#   Show a side-by-side diff of a particular file how it currently exists between:
#       * the file system
#       * in HEAD (latest committed changes)

function usage() {
    cat <<-HERE
    USAGE

    $(basename $1) <file>

    Show a side-by-side diff of a particular file between the current versions:

        * on the file system (latest edited changes)
        * in HEAD (latest committed changes)

HERE
}

if [[ $# = 0 ]]; then
    usage $0
    exit
fi

file=$1
diff -y =(git show HEAD:$file) $file | pygmentize -g | less -R
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文