2

Is it possible to format the output of git diff so I'll get only the actual text difference?

for example, this is the default git diff output:

diff --git a/diff_test.txt b/diff_test.txt
index 6b0c6cf..b37e70a 100644
--- a/diff_test.txt
+++ b/diff_test.txt
@@ -1 +1 @@
-this is a git diff test example
+this is a diff example

Instead I want to get:

diff --git a/diff_test.txt b/diff_test.txt
-this is a git diff test example
+this is a diff example

is it possible with git diff or other git command without using other scripts like shell, python ,etc?

Tidhar Klein Orbach
  • 2,896
  • 2
  • 30
  • 47
  • 1
    Did you read https://git-scm.com/docs/diff-format? – mkrieger1 Sep 10 '20 at 06:58
  • I did, couldn't find a way to get rid of the headers. if you see the answer there, please post as an answer to my question. – Tidhar Klein Orbach Sep 10 '20 at 07:00
  • if you are on bash, you can skip the first 4 header lines by piping to sed. for ex. git diff ... | sed 1,4d – StPiere Sep 10 '20 at 07:45
  • thanks @StPiere , but I'm trying to figure out it it's possible without bash. – Tidhar Klein Orbach Sep 10 '20 at 07:46
  • I dont think there is a git diff option to do that. You have to use a pipe. – StPiere Sep 10 '20 at 07:53
  • "without using bash tricks" seems to indicate you intend to apply it on systems without bash : can you please add your intention in your question (explain the context in which you would like to produce such diffs) ? instead of `sed` (which can also be run from cmd.exe or powershell btw), you can use any scripting language : python or perl, for example, are pretty common on all platforms, and often already installed because other tools need them. – LeGEC Sep 10 '20 at 08:40
  • I meant with using git diff or other git commands and not using other scripts. ("not possible" can also be an answer) – Tidhar Klein Orbach Sep 10 '20 at 08:46

1 Answers1

1

Excluding a diff patch header involves generally shell commands (tail/sed)

You can only limit the context to only the modified line, but anything more, like hiding/remove the hunk headers, as in this answer, will involve some "non-git" command.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250