Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exclude the patch header with "git diff"?

Tags:

git

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?

like image 245
Chris Stryczynski Avatar asked Nov 26 '17 12:11

Chris Stryczynski


People also ask

What is git diff patch?

Git diff is a command used to output the changes between two sources inside the git repository. The data sources can be two different branches, commits, files, etc.

What does ++ mean in git diff?

When viewing a combined diff, if the two files you're comparing have a line that's different from what they were merged into, you will see the ++ to represent: one line that was added does not appear in either file1 or file2.

How do I end git diff?

Use ONLY q+enter to exit. It's possible to break out by repeatedly typing q+enter+q+enter+q+enter until the end of time no matter what the console shows.

How do I use the diff command in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch. Order does matter when you're comparing branches.


3 Answers

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

like image 69
Chris Stryczynski Avatar answered Oct 07 '22 10:10

Chris Stryczynski


git diff etc \
| sed '/^diff /,/^@@/ {
    H;/^@@/!d;z;x
    s".*\n--- a/\([^\n]*.\)+++ b/\1"==> File: \1"
}
'

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

like image 23
jthill Avatar answered Oct 07 '22 08:10

jthill


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.

like image 45
mkrufky Avatar answered Oct 07 '22 08:10

mkrufky