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](https://i-blog.csdnimg.cn/blog_migrate/083c411a7aaf99a38c9c0da5d18815c6.png)
This is especially bad when the lines are long, or there are a lot of very similar lines:
当行很长或有很多非常相似的行时,这尤其糟糕:
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/8fb57fd22d941e6560fd81430ef9afa1.png)
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](https://i-blog.csdnimg.cn/blog_migrate/3d079c56b95cccc79566db8c67666391.png)
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](https://i-blog.csdnimg.cn/blog_migrate/8fb57fd22d941e6560fd81430ef9afa1.png)
![Image for post](https://miro.medium.com/max/9999/1*h8zPouTxZDyvIbRyjTMFqg.png)
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](https://i-blog.csdnimg.cn/blog_migrate/9b9554491efb166bb7ebd169f09c77de.png)
Here is a diff using my custom settings:
这是使用我的自定义设置的差异:
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/874f19320482ac4881b45cd8f3a74f6c.png)
Delta also supports side-by-side diffs:
Delta也支持并排差异:
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/f399638a009b7bfe51d7e9e4fcd43548.png)
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](https://i-blog.csdnimg.cn/blog_migrate/bbe44c20301f40fa865973f74bff0409.png)
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](https://i-blog.csdnimg.cn/blog_migrate/88c95ce66f075230caa67df7f5498eeb.png)
最后的话(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