Got inspiration and so I added a git alias.
$ cat ~/.gitconfig | fgrep diff df = "!git diff -U$(wc -l \"$1\" | cut -d ' ' -f 1) \"$1\"" $ git df <file>
Update in 2023:
Just found git df does not work sometimes, due to directory change when executing git alias. (See git aliases operate in the wrong directory). Also @Moises Soto mentioned on macOS the wc command works differently (6 spaces at the front of the wc -l output) and suggested to use awk. So this is the updated version:
$ cat ~/.gitconfig | fgrep df df = "! [ \"$GIT_PREFIX\" != \"\" ] && cd \"$GIT_PREFIX\"; ~/bin/git_df.sh" $ $ cat ~/bin/git_df.sh #!/bin/bash for FILE in $@; do git diff -U$(wc -l "${FILE}" | awk '{print $1}') "${FILE}" done exit 0
Update on 2024/12/12:
This is the one-liner version I am currently using, which supports multiple files: (Still, add the following into the [alias] section in your .gitconfig)
df = !"diff_one() { cd ${GIT_PREFIX:-.}; git diff --no-ext-diff -U$(wc -l \"$1\" | awk '{print $1}') \"$1\"; cd -; }; f() { for FILE in \"$@\"; do diff_one \"${FILE}\"; done }; f"
you can use it with git df <file1> <file2> ....