git diff_改善git diff

git diff

默认git(Default git)

The default git diff is rather lackluster since it only displays which lines changed, not how they changed:

默认的git diff是比较低迷的,因为它只条线路改变,不是他们如何改变显示:

Image for post
Git diff, default
Git diff,默认

This is especially bad when the lines are long, or there are a lot of very similar lines:

当行很长或有很多非常相似的行时,这尤其糟糕:

Image for post
Long lines and repetition with default diff
长行和重复与默认差异

One solution is looking at diffs using GitHub or GitLab, both of which highlight the specific words that changed. This is a lot nicer, but it obviously doesn’t run in the terminal, requires you to be online, and only works with repos that are hosted on the website (and up to date).

一种解决方案是使用GitHub或GitLab查看差异,两者都突出显示了已更改的特定单词。 这好多了,但是显然它不能在终端中运行,需要您在线,并且只能与网站上托管的仓库(以及最新版本)一起使用。

Image for post
GitHub word-coloring
GitHub单词着色

Thankfully, there is a way to get the same behavior without any of the negatives.

值得庆幸的是,有一种方法可以得到相同的行为而没有任何负面影响。

差异荧光笔 (Diff Highlighters)

Here is the “long lines” example from before, word-colored by a command line tool, compared to the default diff:

这是之前的“长行”示例,与默认的diff相比,该示例由命令行工具用文字上色:

Image for post
Image for post
Long lines and repetition, default vs word-colored
长行和重复,默认值与单词色

This output was produced by diffr, which is most likely available from your operating system’s package manager of choice.

此输出由diffr产生,很可能可以从您所选择的操作系统的程序包管理器中获得。

There are other, more powerful options below, but diffr gives the cleanest output by default.

下面还有其他更强大的选项,但diffr默认提供最干净的输出。

To make Git use diffr, edit your .gitconfig to include the following:

为了让Git的使用diffr ,编辑您.gitconfig到包括以下内容:

# .gitconfig
[pager]
# Use `diffr` (make sure it is installed).
log = diffr | less
show = diffr | less
diff = diffr | less

语法高亮 (Syntax Highlighting)

One thing is still missing when compared to GitHub — Syntax highlighting.

与GitHub相比,仍然缺少一件事-语法突出显示。

Using delta gives you that and much more. The defaults do not look very good, which is why delta is not the first option I suggested, but it is incredibly customizable.

使用delta可为您提供更多信息。 默认值看起来不太好,这就是为什么delta不是我建议的第一个选项的原因,但是它是可定制的。

Here is what it looks like using default settings:

使用默认设置的情况如下所示:

Image for post
`delta` code diff
`delta`代码差异

Here is a diff using my custom settings:

这是使用我的自定义设置的差异:

Image for post
Diff with custom `delta` settings
与自定义`delta`设置不同

Delta also supports side-by-side diffs:

Delta也支持并排差异:

Image for post
Side-by-side diff (default settings)
并排差异(默认设置)

To make Git use delta, edit your .gitconfig to include the following:

要使Git使用delta ,请编辑.gitconfig使其包含以下内容:

# .gitconfig
[pager]
# Use `delta`.
log = delta
show = delta
diff = delta

其他选择 (Other Alternatives)

For the sake of being thorough, here are some other diff highlighters. These rely on a less accurate algorithm for generating the highlights, so they are generally not recommended.

为了更全面,这里有一些其他差异荧光笔。 这些依赖于精度较低的算法来生成高光,因此通常不建议使用它们。

Image for post
diff-highlight word-coloring
diff-highlight字着色

Git is actually bundled a tool — diff-highlight (except on Mac, which thus ships an incomplete Git installation. Sighhh, Apple🤦) (brew install git solves this).

Git实际上是捆绑了一个工具-diff diff-highlight (在Mac上除外,因此它附带了不完整的Git安装。Sighhh,Apple🤦)( brew install git解决了这个问题)。

You can usually find and add it to $PATH at /usr/local/share/git/contrib/diff-highlight/.Then, edit your .gitconfig:

通常您可以在/usr/local/share/git/contrib/diff-highlight/找到并添加到$PATH ,然后编辑.gitconfig

# .gitconfig
[pager]
# Use `diff-highlight` (make sure it is in `$PATH`).
log = diff-highlight | less
show = diff-highlight | less
diff = diff-highlight | less

Finally, you can check out diff-so-fancy (which uses diff-highlight under the hood):

最后,您可以检查diff-so-fancy (它在引擎盖下使用diff-highlight ):

Image for post
diff-so-fancy
差异化

最后的话(Final Words)

Hope this was useful.

希望这是有用的。

Please feel free to reach out on Twitter for any reason.

出于任何原因,请随时与Twitter联系

翻译自: https://medium.com/swlh/improving-git-diffs-4519a6541cd1

git diff

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值