如何在诸如 meld、kdiff3 等差异查看器中查看 `git show` 的输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17558221/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:45:31  来源:igfitidea点击:

How can I view the output of `git show` in a diff viewer like meld, kdiff3, etc

gitversion-controldiffdvcs

提问by Buttons840

There are many SO questions that show how to view the output of a git diffcommand in a diff viewer like meld using git difftoolor otherwise. I am not asking about git diffthough.

有许多 SO 问题展示了如何git diff在 diff 查看器中查看命令的输出,如使用 meldgit difftool或其他方式。我不是在问git diff

I want to see the output of git show <previous commit sha1>in a diff viewer like meld. How can I do this?

我想git show <previous commit sha1>在像meld这样的差异查看器中查看输出。我怎样才能做到这一点?

回答by georgebrock

You can use git difftoolto show a single commit.

您可以使用git difftool来显示单个提交。

Say you want to see the commit with the sha1 abc123:

假设您想查看 sha1 的提交abc123

git difftool abc123~1 abc123

(~1tells git to move to the previous commit, so abc123~1is the commit before abc123)

~1告诉 git 移动到上一个提交,之前的提交abc123~1也是如此abc123

If you use this regularly, you could make a custom git command to make it easier:

如果您经常使用它,您可以自定义 git 命令以使其更容易:

  1. Create a file called git-showtoolsomewhere on your $PATHwith the following contents:

    git difftool ~1 
    
  2. Give that file execute permissions:

    chmod +x ~/path/to/git-showtool
    
  3. Use the command git showtool <sha1 or tag or ...>

  4. Profit.
  1. 使用以下内容创建一个名为git-showtool某处的文件$PATH

    git difftool ~1 
    
  2. 赋予该文件执行权限:

    chmod +x ~/path/to/git-showtool
    
  3. 使用命令 git showtool <sha1 or tag or ...>

  4. 利润。

回答by sagittarian

Building on georgebrock's response, you can create an alias in .gitconfig, something like this:

基于乔治布洛克的回应,您可以在 .gitconfig 中创建一个别名,如下所示:

showtool = "!showci () { rev=${1:-HEAD}; git difftool $rev~1 $rev; }; showci "

Then you can run it with git showtool abc123(without needing to create a separate shell script for this). If you leave out the revision it will default to HEAD.

然后你可以运行它git showtool abc123(不需要为此创建一个单独的 shell 脚本)。如果您省略修订版,它将默认为 HEAD。

回答by Michael Szczepaniak

Translating sagittarian's suggestion for the less git savvy, add this to your global .gitconfigfile which resides in C:\Users[user name]if you are a windows user:

翻译 sagittarian 对 git savvy 的建议,如果您是 Windows 用户,请将其添加到位于C:\Users[user name]的全局.gitconfig文件中:

[alias]
    showtool = "!showci () { rev=${1:-HEAD}; git difftool $rev~1 $rev; }; showci "

or you can execute the following command in the get bash shell:

或者您可以在 get bash shell 中执行以下命令:

git config --global alias.showtool '!showci () { rev=${1:-HEAD}; git difftool $rev~1 $rev; }; showci '

which will make the change to the .gitconfigfile for you.

这将为您更改.gitconfig文件。

回答by John Gliksberg

Building on sagitarrian's answer, here's a small change for more generic rev syntax:

基于 sagitarrian 的回答,这里有一个更通用的 rev 语法的小改动:

showtool = "!showci () { rev=$(git rev-parse \"${*:-HEAD}\"); git difftool $rev~1 $rev; }; showci"

I use rev-parseto transparently allow complex rev expressions.

rev-parse用来透明地允许复杂的 rev 表达式。

I removed the $1at the end, since the alias is called followed by the arguments; it passes unnoticed when using only $1but breaks $*behaviour.

$1在最后删除了,因为调用别名后跟参数;它仅在使用时被忽视,$1但会破坏$*行为。

This allows doing nice things like:

这允许做一些不错的事情,例如:

git showtool :/some nasty bug

回答by MateuszL

Geogrebrock answer is fine, but can be improved thus:

Geogrebrock 的回答很好,但可以改进:

commit=
shift
git difftool $commit~1 $commit $@

Put it in executable (chmod +x) file somewhere in $PATH.

将它放在 $PATH 中某处的可执行 (chmod +x) 文件中。

Now you can pass additional arguments, for example specify what files you want to see (and ignore others):

现在您可以传递其他参数,例如指定您想要查看的文件(并忽略其他文件):

$ git showtool 6a40ec6ffb9338b0548aefab92fded1bffb1648a -- src-cmn/

回答by fiorentinoing

The git-showcommitfile (executable) in the PATH should be as follow:

git-showcommitPATH 中的文件(可执行文件)应如下所示:

#!/bin/bash

git difftool --dir-diff ^..

then you can simply call the new tool by git showcommit HEADor git showcommit a5b26d5

然后你可以简单地通过git showcommit HEAD或调用新工具git showcommit a5b26d5