172

I would like to see a list of files modified since the last commit, as git status shows, but I care only about files located in a single directory. Is there a way to do this? I tried git status <directory>, but it seems this does something completely different (lists all changed files, as they would be if I wrote git add <directory> first).

The documentation for git-status doesn't tell much, apart from the fact that it accepts the same options that git-commit does (but git-commit's purpose isn't to show lists of changed files).

Ajay
  • 18,086
  • 12
  • 59
  • 105
Kuba Suder
  • 7,587
  • 9
  • 36
  • 39

4 Answers4

249

From within the directory:

git status .

You can use any path really, use this syntax:

git status <directoryPath>

For instance for directory with path "my/cool/path/here"

git status my/cool/path/here
Adriano
  • 19,463
  • 19
  • 103
  • 140
Sam Doidge
  • 2,719
  • 1
  • 17
  • 8
  • 4
    Thanks! Also, 'git status ' seems to be doing what I would expect - I honestly don't remember what problem I had with it before... maybe it's a matter of a different git version, dunno. – Kuba Suder Dec 15 '12 at 20:32
  • No problem, and possibly - the newer versions of git do seem to make things easier. – Sam Doidge Dec 17 '12 at 16:12
  • 4
    Yep, behaviour definitely changed from 1.6 to 1.7. It meets the OP requirement in 1.7 but 1.6 continues over the entire repo. – colgur Dec 12 '13 at 23:23
  • 1
    i was looking for this command even after being familiar with most complicated commands like stash and rebase because i simply don't want to see tracked file in red state. git rm also was not useful because it removed from index on commit, but this command is actually one really needed when only need to work with little.Simple solutions always are those which can be seen nowhere. – Shashank Bhatt Sep 11 '20 at 11:55
  • 7
    I had to use `git -C status` – Jake Ireland Jan 27 '21 at 13:43
  • `git diff --stat` – Gjaa Aug 17 '22 at 20:45
35

The reason that git status takes the same options as git commit is that the purpose of git status is to show what would happen if you committed with the same options as you passed to git status. In this respect git status is really git commit --preview.

To get what you want, you could do this which shows staged changes:

git diff --stat --cached -- <directory_of_interest>

and this, which shows unstaged changes:

git diff --stat -- <directory_of_interest>

or this which shows both:

git diff --stat HEAD -- <directory_of_interest>
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 3
    This partially works, although it doesn't show newly created (untracked) files, like git-status does... – Kuba Suder Apr 03 '09 at 20:21
  • OK, I wasn't sure what reports you needed. If you need to check for unstaged files try `git ls-files --others ` or `ls-files -o`. – CB Bailey Apr 03 '09 at 21:25
  • `git ls-files -o` shows files in unstaged directories recursively, whereas `git status` shows only the top-level directory. And one would have to compose `git diff`, `git ls-files` output and recreate all the color coding etc. that `git status` provides if it is to be a replacement for `git status`. I'd also really like to see a solution to this! – Peter V. Mørch Jun 17 '11 at 08:18
14

Simplest solution:

  1. Go to the directory
  2. git status | grep -v '\.\.\/'

Of course this discards colors.

Can Berk Güder
  • 109,922
  • 25
  • 130
  • 137
  • If necessary, you can add some modifications to let the color back. [example 1](https://stackoverflow.com/a/62771672/9935654) [example 2](https://stackoverflow.com/a/62772985/9935654) – Carson Jul 07 '20 at 10:25
2

As a note, if you simplify to check git stats without going to git directory;

### create file
sudo nano /usr/local/bin/gitstat

### put this in

#!/usr/bin/env bash

dir=$1

if [[ $dir == "" ]]; then
    echo "Directory is required!"
    exit
fi

echo "Git stat for '$dir'."

git --git-dir=$dir/.git --work-tree=$dir diff --stat

### give exec perm
sudo chmod +x /usr/local/bin/gitstat

And calling that simple script: gitstat /path/to/foo-project. You can also use it while in foo-project just doing gitstat . and so suppose shorter than git status -s, git diff --stat or git diff --stat HEAD if your are always using console instead of gui's.

Credits:

Community
  • 1
  • 1
Kerem
  • 11,377
  • 5
  • 59
  • 58