Git:从 diff 中的行中删除前导加号/减号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28551556/
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
Git: remove leading plus/minus from lines in diff
提问by caleb531
My question is rather simple, though I have had no luck finding an answer.
我的问题很简单,虽然我没有找到答案。
I'd like to remove the leading plus/minus symbols from each line in git diff
. Before you ask why I wish to do this, allow me to outline my reasons:
我想从git diff
. 在你问我为什么要这样做之前,请允许我概述我的原因:
- Lines that are exactly 80 chars will overflow by a single character, which just looks plain awkward
- The coloring is enough for me to distinguish between additions/deletions
- I'd prefer to keep my Terminal's window width at 80 chars (as opposed to an arbitrary 81 chars) to maintain consistency with everything else I do in my Terminal (outside of
git
)
- 正好是 80 个字符的行将溢出一个字符,这看起来很尴尬
- 着色足以让我区分添加/删除
- 我更愿意将我的终端的窗口宽度保持在 80 个字符(而不是任意的 81 个字符),以保持与我在终端中(在 之外
git
)所做的所有其他事情的一致性
Is there some config option for doing this? If not, how can I do this in a way that still allows me to page through my diff less
-style?
是否有一些配置选项可以执行此操作?如果没有,我如何才能以仍然允许我翻阅差异less
样式的方式执行此操作?
Any insight would be greatly appreciated.
任何见解将不胜感激。
采纳答案by nullptr
One option is to use sed
to remove the undesired character from diff, while preserving the color:
一种选择是使用sed
从 diff 中删除不需要的字符,同时保留颜色:
git diff --color | sed -r "s/^([^-+ ]*)[-+ ]/\1/" | less -r
(Note that you need to remove the leading space as well, as it is emitted by diff.)
(请注意,您还需要删除前导空格,因为它是由 diff 发出的。)
回答by smilyface
The simple way I have seen is this.. Much easy to remember(The text format changes. So you need to know the code change)
我见过的简单方法是这样的..很容易记住(文本格式改变。所以你需要知道代码的变化)
git diff --color-words
git diff --color-words
Here is a way to make it default
If you are using linux add the following command to your ~/.bashrcfile
Then you can use gitdiffwithout spaceas another command .
这是一种使其默认的方法
如果您使用的是 linux 将以下命令添加到您的~/.bashrc文件
然后您可以使用没有空格的gitdiff作为另一个命令。
alias gitdiff='git diff --color-words'
Update:
To set alias directly through git config (Without the help of ~/.bashrc)
https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases
Thank you @dylankb for this.
更新:
直接通过 git config 设置别名(没有 ~/.bashrc 的帮助)
https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases
谢谢@dylankb。
Example:If you enter the command git config --global alias.ci commit
;
then you can use git ci
for rest of your life for committing!!
示例:如果您输入命令git config --global alias.ci commit
;
那么你可以用git ci
你的余生来承诺!!
Happy Gitting :)
快乐的吉丁:)
回答by Kyle Venn
For mac users you'll have to use the below command:
对于 mac 用户,您必须使用以下命令:
git diff --color | sed -E "s/^([^-+ ]*)[-+ ]/\\1/" | less -r
git diff --color | sed -E "s/^([^-+ ]*)[-+ ]/\\1/" | less -r
caleb531provided it in the accepted answer but there was a small typo.
caleb531在接受的答案中提供了它,但有一个小错字。
Then if you want to throw this in an alias you can do the following:
然后,如果您想将其放入别名,您可以执行以下操作:
alias gitdiff='git diff --color | sed -E "s/^([^-+ ]*)[-+ ]/\\1/" | less -r'
alias gitdiff='git diff --color | sed -E "s/^([^-+ ]*)[-+ ]/\\1/" | less -r'
回答by caleb531
If I may answer my own question, I ultimately settled on using a tool called diff-so-fancy. It not only strips the +/- from my diffs, but it also streamlines the file headers andhighlights the changes within each line.
如果我可以回答我自己的问题,我最终决定使用一个名为diff-so-fancy的工具。它不仅从我的差异中去除了 +/-,而且还简化了文件标题并突出显示了每一行中的更改。