If I do git diff
, I'll get the current changes of all unstaged files.
Is there a simpler way to automatically perform git diff
on just the first listed file (using the built in flags)?
The best I've come up with is:
function git_diff_first_unstaged_file() {
if [[ $(git diff --name-only) ]]; then
git diff $(git diff --name-only | sed -n '1 p')
else
echo "gdf: There are currently no unstaged changes."
fi
}
alias gdf='git_diff_first_unstaged_file'
I'm wondering if there is a git diff
flag I brushed over that would do this for me.
Beside git diff --name-only, you also have git ls-files --modified
that would list you unstaged files (to be used with | sed -1p
to get the first one).
Don't forget that if you want to start staging diff for the first file, you have the option to simply type:
git add -p
You will see and will be able to selectively add diffs for the first file (and then the second, at which point you can type q to quit the interactive staging session)
Following the recommendations in the comments, one can add the following alias to ~/.gitconfig
(of project config) in order to make a diff only with the first unstaged but tracked file:
[alias]
gdf = "! git diff $(git diff --name-only | sed -n 1p)"
If you want to make a diff with the i-th unstaged but tracked file:
[alias]
gdi= "!f() { git diff $(git diff --name-only | sed -n $1p); }; f"
git gdi 3
will show diff of unstaged changes of third file (or last file if there are less than 3 files).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With