Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git diff just the first file (of all unstaged changes)

Tags:

git

bash

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.

like image 429
Nico Avatar asked Nov 21 '16 23:11

Nico


2 Answers

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)

like image 200
VonC Avatar answered Oct 03 '22 04:10

VonC


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).

like image 41
x0s Avatar answered Oct 03 '22 04:10

x0s