惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
量子位
博客园 - 司徒正美
V
V2EX
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
N
Netflix TechBlog - Medium
L
LangChain Blog
IT之家
IT之家
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Git Diff Setup
2021-10-24 · via jdhao's digital space

When we run git-diff, git uses its internal diff algorithm to generate diff for changed files.

The git internal diff algorithms#

The default diff algorithm used by git-diff is myers diff. However, myers diff algorithm may not generate meaningful diffs for code changes. So the patience diff and histogram diff algorithm is also added later.

This post shows an example where myers diff does not make much sense and patience or histogram diff produce better diffs. It seems that histogram diff generally produces better diff.

To use patience or histogram diff in git diff, use the following command:

git diff --patience
git diff --histogram

However, this does not mean that histogram/patience diff always generate better diffs. An example is given here1.

f1:

aaaaaa
aaaaaa
bbbbbb
bbbbbb
cccccc
cccccc
abc

f2:

abc
aaaaaa
aaaaaa
bbbbbb
bbbbbb
cccccc
cccccc

The output of git diff --patience f1 f2 is:

diff --git a/f1 b/f2
index 3299d68..accc3bd 100644
--- a/f1
+++ b/f2
@@ -1,7 +1,7 @@
-aaaaaa
-aaaaaa
-bbbbbb
-bbbbbb
-cccccc
-cccccc
 abc
+aaaaaa
+aaaaaa
+bbbbbb
+bbbbbb
+cccccc
+cccccc

The output of git diff f1 f2 (remember that myers diff is used by default) is:

diff --git a/f1 b/f2
index 3299d68..accc3bd 100644
--- a/f1
+++ b/f2
@@ -1,7 +1,7 @@
+abc
 aaaaaa
 aaaaaa
 bbbbbb
 bbbbbb
 cccccc
 cccccc
-abc

In this case, result of myers diff make much more sense than that of patience diff.

Git external diff tools#

Git can also utilize external diff tool via git difftool command. Note that git difftool has nothing to do with git diff (its naming is a bit confusing since it may make the users believe that git difftool chooses which tool git-diff uses).

You can run command git difftool --tool-help to check a list of supported external diff tools. To select a certain tool, use git difftool --tool <my-diff-tool>. For example, to use vimdiff, run git difftool --tool vimdiff.

Refs#