78

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.

caleb531
  • 4,111
  • 6
  • 31
  • 41
  • 2
    The +/- is just diff works. You might need to use a different diff tool if you don't want the +/-. – Roman Feb 16 '15 at 22:42
  • @Dave: some systems have only a terminal (that doesn't run within a graphical shell) - like `tty` - in these terminals can't be resized, simply because they have a fixed raster... – Willem Van Onsem Feb 16 '15 at 23:30
  • 1
    I also would like to know how to disable the leading plus/minuses. Because it really prevents me from copy-pasting code from the diff view. There have been a couple of times where I really just need to copy the code that was changed (to restore some old code) and you just can't do that with the diff tool without doing a search-and-replace after. – birgersp Oct 28 '20 at 09:11

4 Answers4

120

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

smilyface
  • 5,021
  • 8
  • 41
  • 57
  • 5
    Is there some way to make this the default? – ioquatix Feb 25 '16 at 06:27
  • 3
    If you are using linux add the command inside the bracket to your ~/.bashrc file (alias gitdiff='git diff --color-words') . Then you can use gitdiff without space as another command . – smilyface Feb 28 '16 at 08:44
  • 11
    This does more than removing the +/- for me. This also puts some additions/removals on the same line unfortunately. – Pat Myron Nov 01 '17 at 00:47
  • 1
    @ioquatix You can also use git config - both the `git config` cli and the `~/.gitconfig` file. If you use git config you probably can't make it a single word like you can with an bash alias or overwrite `diff`, but you definitely make something short like `git d` by setting `d = diff --color` in the `[alias]` in ~/.gitconfig. Here's docs for the cli. https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases – dylankb Jan 08 '20 at 23:22
  • Woops, I mean `d = diff --color-words` – dylankb Jan 08 '20 at 23:28
  • Thank you @dylankb, I will update the post with the link you provided. Let it help others. – smilyface Jan 10 '20 at 05:53
  • 1
    `git show --color-words` also works – tee Mar 23 '23 at 15:38
44

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

nullptr
  • 11,008
  • 1
  • 23
  • 18
  • 11
    This works for me, though I needed to change `sed -r` to sed -E` (I'm running OS X, so I assume it's some sort of platform-related quirk). Anyway, this solution works nicely as a git alias—thanks very much :) – caleb531 Feb 17 '15 at 00:20
  • I have an alias in my .profile: alias nm='sed -r "s/^([^-+ ]*)[-+ ]/\\1/"' and I also did: git config alias.df='diff --color' So now I can do: git diff [whatever parameters I want] | nm to get rid of all the pluses and minuses, retaining the color. ("nm" stands for "no minus") – JoelFan Feb 13 '17 at 20:46
  • 2
    this option is better than `--color-words` because it keeps the changes on separate lines. some users don't want to concatenate changes that occur on the same line. this removes the plus and minus while preserving the line breaks. – brianyang Jun 22 '17 at 00:54
  • the answer below with `git diff --color-words` was better suited for me eventhough the changes can be squeezed into one line. – steffres Mar 10 '21 at 04:16
8

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'

smilyface
  • 5,021
  • 8
  • 41
  • 57
Kyle Venn
  • 4,597
  • 27
  • 41
8

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

caleb531
  • 4,111
  • 6
  • 31
  • 41
  • Good choice. I'll also suggest the use of `less -S` in a pinch in situations where wrapping can be a hassle. Just don't think that that warrants its own answer. – Steven Lu Mar 09 '21 at 01:50