Git: remove leading plus/minus from lines in diff

GitGit Diff

Git Problem Overview


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:

  1. Lines that are exactly 80 chars will overflow by a single character, which just looks plain awkward
  2. The coloring is enough for me to distinguish between additions/deletions
  3. 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)

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?

Any insight would be greatly appreciated.

Git Solutions


Solution 1 - Git

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'


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.

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!!

Happy Gitting :)

Solution 2 - Git

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

Solution 3 - Git

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'

Solution 4 - Git

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.

diff-so-fancy

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncaleb531View Question on Stackoverflow
Solution 1 - GitsmilyfaceView Answer on Stackoverflow
Solution 2 - GitnullptrView Answer on Stackoverflow
Solution 3 - GitKyle VennView Answer on Stackoverflow
Solution 4 - Gitcaleb531View Answer on Stackoverflow