Is there a way I can dictate formatting to the output of git diff
? When there are multiple files, I'd like some extra blank lines before each file start — that is, before each time it says
diff --git a/filename b/filename
This would help me spot the individual files. Right now I'm piping into BBEdit and then doing a global find-and-replace, and it's getting very old very fast. I don't really need to use a diff tool here, I think; I just want a little control over the text output. (Telling me how to pipe it through sed
or something would probably be cool, though I was sort of hoping that git diff
itself might accept some sort of formatting option.)
Here is an answer using sed and bash/zsh (as answered here) :
# include a '\' followed by a linefeed ('\n') in the replace pattern of sed :
git diff --cached | sed -e 's/^diff --git/'$'\\\ndiff --git/'
(note: the bash/zsh specific part of the command above lies in the syntax used to insert an actual \<lf>
on the command line : $'\\\n'
will translate to that in bash/zsh, but perhaps not in other shells -- e.g : cmd.exe or powershell. You may choose another way to insert a \<lf>
in a sed pattern, for example by reading patterns from a file instead of the command line, or by choosing a shell specific way to insert those characters on the command line)
If you want to keep the colors displayed by git diff
, one way to do so is :
git diff --cached --color=always |\
# spot the escape sequence that sets "diff --git" in bold, and print it in the output
sed -e 's/^\(..1m\)diff --git/'$'\\\n\\1diff --git/'