我能否要求git仅显示已修改的行并忽略所有未修改的代码?
并且从
主答案下OP的跟进评论:
谢谢您的快速回复。这解决了我的一半问题,但我仍然会得到一些像@@-1+1@@这样的行在我的diff中,并且我的git diff顶部有diff--git a/db/xxxxxxx b/db/xxxx索引xxxxx..aaaaaaa bbbbbbbb。-r3b00t
为了解决上述两个请求,这是使用我编写的基于awk语言的git-diffc.sh包装器的1行解决方案:
git diffc
完成!
这里是git diffc
的特点。
所有这些特点,综合起来,解决了其他答案中存在的缺陷:
- It handles color AND no-color output. That's what this regular expression does:
^(\033\[(([0-9]{1,2};?){1,10})m)?
- It handles ALL COLORS and ALL TEXT FORMATTING OPTIONS, including bold, italics, strikethrough, etc, which you can set in your
git config
settings. That's why the regex above has ;?
and {1,10}
in it: if it detects the start of a color or text formatting code, it will match up to 10 sequences of these combined ANSI codes.
- It does NOT also include lines which begin with
@@
and the word diff
, as the accepted answer does. If you DO want those lines (which quite frankly, I think are useful :) ), do this instead:
git diff --unified=0
or
git diff -U0
- It shows the output in the same exact way as
git diff
would: in the less
pager with optional color output (-R
), and only if the text is > 1 page (-F
), and while retaining the current page of text on the screen when you q
uit (-X
).
它还有一个优点,就是使用awk编程语言,因此功能强大且易于配置。
git diff 8d4d4fd3b60f200cbbb87f2b352fb097792180b2~2..8d4d4fd3b60f200cbbb87f2b352fb097792180b2~3
的示例输出:
diff --git a/useful_scripts/rg_replace.sh b/useful_scripts/rg_replace.sh
index 74bc5bb..0add69d 100755
--- a/useful_scripts/rg_replace.sh
+++ b/useful_scripts/rg_replace.sh
@@ -2,12 +2,11 @@
-# STATUS: functional and ready-to-use
-
+# WORK IN PROGRESS! <===========
-# flag will modify your files." This wrapper overcomes that limitation.
+# flag will modify your files."
与git diffc 8d4d4fd3b60f200cbbb87f2b352fb097792180b2~2..8d4d4fd3b60f200cbbb87f2b352fb097792180b2~3
的样本输出相比较。注意,只有-
和+
行被显示出来,周围的上下文行消失了,而所有其他的行,如diff
、index
、---
、+++
和@@
也都消失了!:
-# STATUS: functional and ready-to-use
-
+# WORK IN PROGRESS! <===========
-# flag will modify your files." This wrapper overcomes that limitation.
+# flag will modify your files."
git diffc
代表“git diff changes”,意思是:仅显示已更改的代码行,不显示其他内容。我编写了它。 它不是常规git的一部分。
它支持git diff
支持的所有选项和参数,因为它只是git diff
的轻量级包装器。
在此处下载: git-diffc.sh. 它是我的eRCaGuy_dotfiles repo的一部分。
要安装它:
git clone https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles.git
cd eRCaGuy_dotfiles/useful_scripts
mkdir -p ~/bin
ln -si "${PWD}/git-diffc.sh" ~/bin/git-diffc
如果这是您第一次创建或使用~/bin
目录,请手动注销并重新登录,以使Ubuntu默认的~/.profile
文件将~/bin
添加到您的PATH
变量中。如果注销并重新登录无效,请将以下几行代码添加到您的~/.profile
文件中,然后再次注销Ubuntu并重新登录:
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
就是这样!
用法:与 git diff
相同。例如:
git diffc
git diffc -h
git diffc commit1 commit2
git diffc --no-color
安装补充说明:
另请参阅我关于 git diffn
的安装部分的其他答案,该答案也是我编写的,在这里。除此之外,你需要在那些说明中的任何地方使用 git-diffc
替换 git-diffn
。这也包括 wget
命令内部。下载和安装 git diffc
很容易:只需执行几个命令即可。
如何使用 awk
仅显示 +
和 -
行,考虑到 git diff
可能输出的任何颜色或文本格式:
下面的代码是构成 git diffc
包装器的内容。
这里的其他答案(包括我的另一个答案)都不能完全满足您的要求。然而,这个答案可以。以下是一个可以复制粘贴到终端的一行代码,我将其分成多行以提高可读性--无论哪种方式您都可以复制粘贴它,所以我不妨让它更易读! 它依赖于awk
编程语言:
git diff --color=always "$@" | awk '
# 1. Match and then skip "--- a/" and "+++ b/" lines
/^(\033\[(([0-9]{1,2};?){1,10})m)?(--- a\/|\+\+\+ b\/)/ {
next
}
# 2. Now print the remaining "+" and "-" lines ONLY! Note: doing step 1 above first was required or
# else those lines would have been matched by this matcher below too since they also begin with
# the "+" and "-" symbols.
/^(\033\[(([0-9]{1,2};?){1,10})m)?[-+]/ {
print $0
}
' | less -RFX
如果你有兴趣学习awk,这里有一些相关资源:
gawk
(GNU awk
)手册:https://www.gnu.org/software/gawk/manual/html_node/index.html#SEC_Contents
- 学习
git diffn
以及其中的注释:https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_scripts/git-diffn.sh
- 如果您也想要
git diffn
,它是带有行号的git diff
,请参见此处:带有行号的Git diff(带有行号的Git日志)
- 一些awk“hello world”和语法测试示例:https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/tree/master/awk
作为奖励,我还将上述内容包装成
git diffc
供使用,意思是“仅显示‘更改’的git diff”。用法与
git diff
完全相同;只需使用
git diffc
即可!它支持所有选项。默认情况下为彩色。要关闭它,只需使用
git diffc --no-color
或
git diffc --color=never
。有关详细信息,请参见
man git diff
。
由于昨晚刚完成了
git diffn
(一个显示带有行号的
git diff
的工具),编写
git diffc
非常简单。我想在知识还新鲜的时候就把它做好。
git diffc
。 - Gabriel Staples