git command to diff the nth modified file of a git status output

Viewed 153

I often run a git status and then want to see the specific diffs of one of those files. So after:

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:  path/to/modified/file1
    modified:  path/to/modified/file1

Is it possible with git, or as a fallback bash, to git diff the nth modified file, so that git diff <someMagic(2)> would reduce to git diff path/to/modified/file2.

my main question is if this exists within git, and if not to help with a bash command.

1 Answers

If automation is not your goal, then I would say that git add -i would fit your case.

Have a look at git add -i documentation.

If you're looking for a script then I've done something that does exactly what you want:

#!/bin/bash
git diff $(git status | grep modified | sed 's/^.*:   //' | head -n "$1" | tail -n 1)

The way to use it is to call it and then pass the number of the modified file you want to diff: <script_name> N, where N is number of the "nth modified file".

(e.g. using your example, if I call <script>.sh 2 it would call diff like this: git diff path/to/modified/file2)

Related