Git: remove leading plus/minus from lines in diff

折月煮酒 提交于 2019-11-29 21:24:55

One option is to use sed to remove the undesired character from diff, while preserving the color:

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.)

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



Here is a way to make it default
If you are using linux add the following command to your ~/.bashrc file
Then you can use gitdiff without space as another command .

alias gitdiff='git diff --color-words'
Kyle Venn

For mac users you'll have to use the below command:

git diff --color | sed -E "s/^([^-+ ]*)[-+ ]/\\1/" | less -r

caleb531 provided it in the accepted answer but there was a small typo.

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'

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 and highlights the changes within each line.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!