14

For example in the below output of git diff

diff --git a/commands.txt b/commands.txt
index 79e881a..a9588e5 100644
--- a/commands.txt
+++ b/commands.txt
@@ -1,3 +1,7 @@
+this is an example
+abcxyz
+helllo
+wooo
 makeFilePermissionExecutable
 makeOwnedByMyself
 makeFilePermissionEverything

Is it possible to hide the following:

diff --git a/commands.txt b/commands.txt
index 79e881a..a9588e5 100644
--- a/commands.txt
+++ b/commands.txt

And instead just show the filename (commands.txt in this case) instead?

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
  • The answer to the question can be "`git diff` has no such an option, so you have to find some workaround." And the answer by @mkrufky is quite an adequate workaround. – Bálint Sass Apr 14 '21 at 11:48

3 Answers3

7

git diff | tail -n +5 will produce the output that you desire.

We pipe the output of git diff into tail -n +5 to begin output on line 5. See the man page for tail -n:

   -n, --lines=[+]NUM
          output the last NUM lines, instead of the last  10;  or  use  -n
          +NUM to output starting with line NUM

You'll have to do some additional regex work if you're looking to consolidate --- a/commands.txt and +++ b/commands.txt to a single line.

mkrufky
  • 3,268
  • 2
  • 17
  • 37
  • assigning downvotes without an explanation to people trying to provide answers to help you won't score you very many points. – mkrufky Nov 26 '17 at 13:07
  • 6
    This answer will not work appropriately when multiple files are changed. – Chris Stryczynski Nov 26 '17 at 15:06
  • I see what you mean. `git diff` itself doesn't offer any option to show the diff while completely omitting all headers. However, `git` is open source. You can add the functionality that you want to your own fork of the git repository, located at https://github.com/git/git – mkrufky Nov 26 '17 at 15:41
  • 6
    That may be true but then the answer to every single question of the form "can I make software X do Y" when X is open source is going to be "yes". I'm pretty sure the OP's question came with the intrinsic clause of "without writing my own git client". – Lasse V. Karlsen Dec 02 '17 at 20:03
4
git diff etc \
| sed '/^diff /,/^@@\|Binary/ {
    H;/^@@\|Binary/!d;z;x
    s".*\n--- a/\([^\n]*.\)+++ b/\1"==> File: \1"
}
'

That's "accumulate all lines in blocks starting ^diff and ending ^@@ or ^Binary, and if the file isn't new or binary reformat the header".

jthill
  • 55,082
  • 5
  • 77
  • 137
3

A workaround I found:

Make the following file accessible in your PATH:

customGitDiff

#!/usr/bin/env bash

echo "$(tput setaf 4) $1"
echo -n "$(tput setaf 4)"
printf "%$(tput cols)s"|tr ' ' '-'
diff -u "$1" "$2" | tail -n +3 | colordiff
echo ""

Then you can instruct git to use the following script as your diff tool. It can be globally set by setting the following in your ~/.gitconfig:

[diff]
  external = customGitDiff

One possibly unfortunate limitation of this, is I'm not sure if it uses the same diff algorithm as git diff.

The above provides me with the following output: enter image description here

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286