git 在每个更改的行中突出显示更改的行和更改的字节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5326008/
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
Highlight changed lines and changed bytes in each changed line
提问by Nikolay Frantsev
Open Source project Trachas an excellent diff highlighter — it highlights changed lines and changed bytes in each changed line!See hereor herefor examples.
开源项目Trac有一个出色的差异高亮器——它高亮显示更改的行和每个更改行中更改的字节!有关示例,请参见此处或此处。
Is there way to use the same color highlight (i.e. changed lines and changed bytes too) in bash terminal, git
, or vim
for diff output (patch-file)?
有没有办法在 bash 终端或差异输出(补丁文件)中使用相同颜色的高亮显示(即更改行和更改字节)?git
vim
回答by Sina Samavati
The diff-highlight
Perl contrib scriptproduces output so similar to that of the Trac screenshots that it is likely that Trac is using it:
在diff-highlight
Perl中的contrib脚本产生输出,类似于Trac的截图,它很可能是Trac的使用它:
Install with:
安装:
wget https://raw.githubusercontent.com/git/git/fd99e2bda0ca6a361ef03c04d6d7fdc7a9c40b78/contrib/diff-highlight/diff-highlight && chmod +x diff-highlight
Move the file diff-highlight
to the ~/bin/
directory (or wherever your $PATH
is), and then add the following to your ~/.gitconfig
:
将文件移动diff-highlight
到~/bin/
目录(或您$PATH
所在的任何位置),然后将以下内容添加到您的~/.gitconfig
:
[pager]
diff = diff-highlight | less
log = diff-highlight | less
show = diff-highlight | less
Single copy paste install suggested by @cirosantilli:
@cirosantilli 建议的单一复制粘贴安装:
cd ~/bin
curl -O https://raw.githubusercontent.com/git/git/fd99e2bda0ca6a361ef03c04d6d7fdc7a9c40b78/contrib/diff-highlight/diff-highlight
chmod +x diff-highlight
git config --global pager.log 'diff-highlight | less'
git config --global pager.show 'diff-highlight | less'
git config --global pager.diff 'diff-highlight | less'
回答by anydot
While using git diff
or git log
and possibly others, use option --word-diff=color
(there are also other modes for word diffs BTW)
在使用git diff
或git log
和可能的其他人时,使用选项--word-diff=color
(顺便说一句,单词差异也有其他模式)
回答by Tom Hale
diff-so-fancy
is a diff
-highlighter designed for human eyeballs.
diff-so-fancy
是diff
专为人类眼球设计的荧光笔。
It removes the leading +
/-
which are annoying for cut/paste and makes clear sections between files.
它删除了对剪切/粘贴很烦人的前导+
/-
并在文件之间制作清晰的部分。
Coloured git
(left) vs diff-so-fancy
(right - note the character-level highlights):
彩色git
(左)与diff-so-fancy
(右 - 注意角色级别的亮点):
If you want thediff-so-fancy
(right side) output but not constrained to files in a git
repository, add the following function to your .bashrc
to use it on any files:
如果您想要diff-so-fancy
(右侧)输出但不限于git
存储库中的文件,请将以下函数添加到您的.bashrc
任何文件中:
dsf() { git diff --no-index --color "$@" | diff-so-fancy; }
Eg:
例如:
dsf original changed-file
Character level highlighting and standard diff
format
字符级高亮和标准diff
格式
If you don't like the non-standard formatting of diff-so-fancy
, but still want character-level git
highlighting, use diff-highlight
which will take git
's output and produce the really pretty standard diff
-format output:
如果您不喜欢 的非标准格式diff-so-fancy
,但仍需要字符级git
突出显示,请使用diff-highlight
which 将获取git
的输出并生成非常漂亮的标准diff
格式输出:
To use it by default from git
, add to your .gitconfig
:
要默认从 使用它git
,请添加到您的.gitconfig
:
[color "diff-highlight"]
oldNormal = red bold
oldHighlight = red bold 52
newNormal = green bold
newHighlight = green bold 22
[pager]
diff = diff-highlight | less -FRXsu --tabs=4
The [pager]
section tells git
to pipe its already colourised output to diff-highlight
which colourises at the character level, and then pages the output in less (if required), rather than just using the default less
.
该[pager]
部分告诉git
将其已经着色的输出通过管道传输到diff-highlight
字符级别的哪些着色,然后将输出分页较少(如果需要),而不是仅使用默认的less
.
回答by dshepherd
The behaviour you want is now available in git itself (as was pointed out in a comment by naught101). To enable it you need to set your pager to
您想要的行为现在在 git 本身中可用(正如 naught101 在评论中指出的那样)。要启用它,您需要将寻呼机设置为
perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less
where /usr/share/doc/git/contrib/diff-highlight/diff-highlight
is the location of the highlighter script on Ubuntu 13.10 (I have no idea why it's in a doc
folder). If it isn't there on your system try using locate diff-highlight
to find it. Note that the highlighting script is not executable (at least on my machine), hence the requirement for perl
.
/usr/share/doc/git/contrib/diff-highlight/diff-highlight
Ubuntu 13.10 上荧光笔脚本的位置在哪里(我不知道为什么它在doc
文件夹中)。如果您的系统上没有它,请尝试使用locate diff-highlight
它来查找它。请注意,突出显示脚本不可执行(至少在我的机器上),因此需要perl
.
To always use the highlighter for the various diff-like commands just add the following to your ~/.gitconfig
file:
要始终将荧光笔用于各种类似 diff 的命令,只需将以下内容添加到您的~/.gitconfig
文件中:
[pager]
log = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less
show = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less
diff = perl /usr/share/doc/git/contrib/diff-highlight/diff-highlight | less
I added this as a new answer naught101's comment is buried and because the set up is not quite as trivial as it should be and at least on the version of Ubuntu that I have the instructions in the READMEdon't work.
我添加了这个作为一个新答案 naught101 的评论被埋没了,因为设置并不像它应该的那样微不足道,至少在 Ubuntu 版本上,我在自述文件中的说明不起作用。
回答by Cory Klein
A utility for byte-based diffs has been distributed with official Git since v1.7.81. You just have to locate where it is installed on your machine and enable it.
自 v1.7.8 1以来,基于字节的差异的实用程序已随官方 Git 分发。您只需要找到它在您的机器上的安装位置并启用它。
Find where Git is installed
查找 Git 的安装位置
- MacOS with Git installed via Homebrew: It's
/usr/local/opt/git
- Windows with Git for Windows: Run
cd / && pwd -W
to find the install directory. - Linux: Nerd. If you don't already know where Git is installed, then
ll $(which git)
orlocate git
should help.
- 通过Homebrew安装 Git 的 MacOS :
/usr/local/opt/git
- Windows with Git for Windows:运行
cd / && pwd -W
以查找安装目录。 - Linux:书呆子。如果您还不知道 Git 安装在哪里,那么
ll $(which git)
或locate git
应该有所帮助。
Link diff-highlight
to your bin directory so that your PATH can find it
链接diff-highlight
到您的 bin 目录,以便您的 PATH 可以找到它
GIT_HOME='/usr/local/opt/git/' # Use the value from the first step.
ln -s "${GIT_HOME}/share/git-core/contrib/diff-highlight/diff-highlight" \
'/usr/local/bin/diff-highlight'
Enable it in your Git config
在您的 Git 配置中启用它
git config --global interactive.diffFilter diff-highlight # Use on interactive prompts
git config --global pager.diff "diff-highlight | less" # Use on git diff
git config --global pager.log "diff-highlight | less" # Use on git log
git config --global pager.show "diff-highlight | less" # Use on git show
1Here is the v1.7.8 version, but lots of changeshave been made since then.
回答by amized
I use --color-words
option and it works fine for me :
我使用--color-words
选项,它对我来说很好用:
$ git diff --color-words | less -RS
回答by Eugen Konkov
as @dshepherdsays:
正如@dshepherd所说:
The behaviour you want is now available in git itself
你想要的行为现在在 git 本身中可用
But diff-highlight
is located in DOC and is not available from shell.
To install diff-highlight
into your ~/bin
directory follow next steps (This will save your typing):
但diff-highlight
位于 DOC 中,无法从 shell 获得。
要安装diff-highlight
到您的~/bin
目录中,请按照以下步骤操作(这将保存您的输入):
$ locate diff-highlight
$ cd /usr/share/doc/git/contrib/diff-highlight #or path you locate
$ sudo make
$ mv diff-highlight ~/bin
Then configure your .gitconfig
as official doc says:
然后配置你的.gitconfig
官方文档说:
[pager]
log = diff-highlight | less
show = diff-highlight | less
diff = diff-highlight | less
UPD
Also you can try next on latest git
without any installation:
UPD
您也可以在git
不进行任何安装的情况下尝试最新版本:
git diff --color-words=.
More complex:
更复杂:
git diff --color-words='[^[:space:]]|([[:alnum:]]|UTF_8_GUARD)+'
回答by Finbar Crago
Emacs has the ediff-patch-buffer function which should fulfill your needs.
Emacs 具有 ediff-patch-buffer 功能,应该可以满足您的需求。
Open the un-patched file in emacs type ESC-x, ediff-patch-buffer.
在 emacs 类型 ESC-x、ediff-patch-buffer 中打开未打补丁的文件。
Follow the prompts and you should see a highlighted comparison of the patched and original versions of your file.
按照提示操作,您应该会看到文件的修补版本和原始版本的突出显示比较。
As per your comment the following will will give you a bash solution requiring only dwdiff:
根据您的评论,以下将为您提供仅需要 dwdiff 的 bash 解决方案:
#!/bin/bash
paste -d'\n' <(dwdiff -2 -L -c <(cat ) <(patch -i -o -)) <(dwdiff -1 -L -c <(cat ) <(patch -i -o -))| uniq
回答by PDug
Yes, Vim does this including the highlighting of text changed within a line.
See :h diff
and :h 08.7
for more details on how to diff files.
是的,Vim 会这样做,包括突出显示一行内更改的文本。
有关如何区分文件的更多详细信息,请参阅:h diff
和:h 08.7
。
Vim uses a fairly simple algorithm for it's highlighting.
It searches the line for the first changed character, and then the last changed character, and simply highlights all characters between them.
This means you can't have multiple highlights per line - many design decisions in Vim prioritise efficiency.
Vim 使用一种相当简单的算法来突出显示。它在行中搜索第一个更改的字符,然后是最后一个更改的字符,并简单地突出显示它们之间的所有字符。
这意味着每行不能有多个突出显示 - Vim 中的许多设计决策优先考虑效率。
回答by Xavier T.
vimdiff file1 file2
will display the difference character-wise between two files.
vimdiff file1 file2
将显示两个文件之间的字符差异。
vimdiff is a diff tool included into vim. (Vim should have been compiled with the +diff option, to be sure you can check with :version
)
vimdiff 是一个包含在 vim 中的差异工具。(Vim 应该使用 +diff 选项编译,以确保您可以检查:version
)
You can also launch it from inside vim.
See :help diff
for more information and commands.
您也可以从 vim 内部启动它。有关:help diff
更多信息和命令,请参阅。