Can I ask git to show only lines that have been modified and ignore all other code which has not been modified?
And from the OP's follow-up comment under the main answer:
Thank you for quick reply. This solves half of my problem but I am still getting some lines like @@ -1 +1 @@ in my diff and top of my git diff have diff --git a/db/xxxxxxx b/db/xxxx index xxxxx..aaaaaaa bbbbbbbb. -r3b00t
To solve both of those requests above, here is the 1-line solution using the git-diffc.sh awk-language-based wrapper I wrote around git diff:
git diffc
Done!
Here are the features of git diffc.
All these features, when taken together, solve the shortcomings of every other answer here:
- It handles color AND no-color output. That's what this regular expression does: ^(\033\[(([0-9]{1,2};?){1,10})m)?
- It handles ALL COLORS and ALL TEXT FORMATTING OPTIONS, including bold, italics, strikethrough, etc, which you can set in your git configsettings. That's why the regex above has;?and{1,10}in it: if it detects the start of a color or text formatting code, it will match up to 10 sequences of these combined ANSI codes.
- It does NOT also include lines which begin with @@and the worddiff, as the accepted answer does. If you DO want those lines (which quite frankly, I think are useful :) ), do this instead:git diff --unified=0
 orgit diff -U0
 
- It shows the output in the same exact way as git diffwould: in thelesspager with optional color output (-R), and only if the text is > 1 page (-F), and while retaining the current page of text on the screen when youquit (-X).
It also has the benefit of being powerful and easily configurable since it uses the awk programming language.
Sample output of git diff 8d4d4fd3b60f200cbbb87f2b352fb097792180b2~2..8d4d4fd3b60f200cbbb87f2b352fb097792180b2~3:
diff --git a/useful_scripts/rg_replace.sh b/useful_scripts/rg_replace.sh
index 74bc5bb..0add69d 100755
--- a/useful_scripts/rg_replace.sh
+++ b/useful_scripts/rg_replace.sh
@@ -2,12 +2,11 @@
 
 # This file is part of eRCaGuy_dotfiles: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
 
-# STATUS: functional and ready-to-use
-
+# WORK IN PROGRESS! <===========
 # This is a simple wrapper around RipGrep (`rg`) to allow in-place find-and-replace, since the
 # `rg --replace` option replaces only the stdout, NOT the contents of the file.
 # `man rg` under the `--replace` section states: "Neither this flag nor any other ripgrep
-# flag will modify your files." This wrapper overcomes that limitation.
+# flag will modify your files."
 
 # INSTALLATION INSTRUCTIONS:
 # 1. Install RipGrep: https://github.com/BurntSushi/ripgrep#installation
versus the sample output of git diffc 8d4d4fd3b60f200cbbb87f2b352fb097792180b2~2..8d4d4fd3b60f200cbbb87f2b352fb097792180b2~3. Notice that only - and + lines are shown, whereas surrounding context lines are gone, and all other lines such as diff, index, ---, +++, and @@ are gone too!:
-# STATUS: functional and ready-to-use
-
+# WORK IN PROGRESS! <===========
-# flag will modify your files." This wrapper overcomes that limitation.
+# flag will modify your files."
git diffc stands for "git diff changes", meaning: show just the changed lines of code, nothing else. I wrote it. It is not part of regular git.
It supports ALL options and parameters supported by git diff, since it's just a light-weight wrapper around git diff.
Download it here: git-diffc.sh. It is part of my eRCaGuy_dotfiles repo.
To install it:
git clone https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles.git
cd eRCaGuy_dotfiles/useful_scripts
mkdir -p ~/bin
ln -si "${PWD}/git-diffc.sh" ~/bin/git-diffc
Now manually log out and log back in now if this is the first time you've ever created or used the ~/bin dir, in order to cause Ubuntu's default ~/.profile file to add ~/bin to your PATH variable. If logging out and back in doesn't work, add the following few lines of code to your ~/.profile file and then log out of Ubuntu and log back in again:
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi
That's it!
Usage: same as git diff. Ex:
git diffc
git diffc -h
git diffc commit1 commit2
git diffc --no-color
# etc.
Additional installation notes:
See also the installation section of my other answer about git diffn, which I also wrote, here. Except, everywhere you see git-diffn in those instructions, use git-diffc instead. That includes inside the wget command too. Downloading and installing git diffc is easy: it's just a few commands.
How to use awk to show just the + and - lines, accounting for any color or text formatting git diff may be outputting:
This code below is what makes up the git diffc wrapper.
Not a single one of the other answers here (including my other answer) will do exactly what you want 100% correctly. This answer, however, will. Here is a 1-liner you can copy and paste into your terminal. I've just made it multiple lines for readability--you can copy-paste it the same either way so I might as well make it readable! It relies on the awk programming language:
git diff --color=always "$@" | awk '
# 1. Match and then skip "--- a/" and "+++ b/" lines
/^(\033\[(([0-9]{1,2};?){1,10})m)?(--- a\/|\+\+\+ b\/)/ {
    next 
} 
# 2. Now print the remaining "+" and "-" lines ONLY! Note: doing step 1 above first was required or
# else those lines would have been matched by this matcher below too since they also begin with 
# the "+" and "-" symbols.
/^(\033\[(([0-9]{1,2};?){1,10})m)?[-+]/ {
    print $0 
}
' | less -RFX
If you are interested in learning awk, here are some resources:
- gawk(GNU- awk) manual: https://www.gnu.org/software/gawk/manual/html_node/index.html#SEC_Contents
- Study git diffnand the comments therein: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_scripts/git-diffn.sh
- If you want git diffntoo, which isgit diffwith line numbers, see here: Git diff with line numbers (Git log with line numbers)
- Some awk "hello world" and syntax test examples: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/tree/master/awk
As a bonus, I also wrapped up the above to be used as git diffc, which means "git diff to show ONLY 'c'hanges". Usage is identical to git diff; just use git diffc instead! It supports ALL options. Color is ON by default. To turn it off, simply use git diffc --no-color or git diffc --color=never. See man git diff for details.
Since I just finished git diffn (a tool to show git diff with line 'n'umbers) last night, writing git diffc was trivial. I figured I better do it now while the knowledge is fresh in my head.