98

When I do git status in a subfolder of my repository it includes the status of parent folders also.

Is there a way to constrain git-status to just a particular folder?

EoghanM
  • 25,161
  • 23
  • 90
  • 123
  • 1
    no, "git status ." gives sibling folders as e.g. "../sibling" – EoghanM Jun 26 '09 at 12:56
  • Would you mind giving a little more context about why you want to filter out changes elsewhere in your tree? In what situations would you like to use this feature? – Greg Bacon Jun 27 '09 at 16:34
  • 1
    If there is more than a screenfull worth of output from the normal "git status" command – EoghanM Jun 29 '09 at 07:47
  • 3
    possible duplicate of [git status - is there a way to show changes only in a specific directory?](http://stackoverflow.com/questions/715321/git-status-is-there-a-way-to-show-changes-only-in-a-specific-directory) – pal4life Jul 18 '14 at 19:27

7 Answers7

132
git status .

will show the status of the current directory and subdirectories.

For instance, given files (numbers) in this tree:

a/1
a/2
b/3
b/4
b/c/5
b/c/6

from subdirectory "b", git status shows new files in the whole tree:

% git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   ../a/1
#   new file:   ../a/2
#   new file:   3
#   new file:   4
#   new file:   c/5
#   new file:   c/6
#

but git status . just shows files in "b" and below.

% git status .
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   3
#   new file:   4
#   new file:   c/5
#   new file:   c/6
#

Just this subdirectory, not below

git status . shows all files below "b" recursively. To show just the files in the "b" but not below, you need to pass a list of just the files (and not directories) to git status. This is a bit fiddly, depending on your shell.

Zsh

In zsh you can select ordinary files with the "glob qualifier" (.). For example:

% git status *(.)
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   3
        new file:   4

Bash

Bash doesn't have glob qualifiers but you can use GNU find to select ordinary files and then pass them along to git status like so:

bash-3.2$ find . -type f -maxdepth 1 -exec git status {} +
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   3
        new file:   4

This uses -maxdepth which is a GNU find extension. POSIX find doesn't have -maxdepth, but you can do this:

bash-3.2$ find . -path '*/*' -prune -type f -exec git status {} +
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   3
        new file:   4
Matt Curtis
  • 23,168
  • 8
  • 60
  • 63
  • 2
    Sorry took so long to come back to this answer - simple and correct! I wonder if was added to git status subsequent to my question? – EoghanM Sep 05 '12 at 15:26
  • What if you are only interested in the current folder where `.` refers to but not its sub folders. Any work around for that? Thanks! – benjaminz Jan 06 '17 at 16:09
  • @benjaminz depends on your use -- for things like that I use shell globbing to select files but not sub-directories, e.g. (for zsh) `git status *(.)`. Bash doesn't have a shorthand way of globbing only files, but you could use an extended `find` e.g. `find . -type f -maxdepth 1 -exec git status {} +`. Seems like the sort of thing you'd want to put in an alias or script rather than type out all the time. – Matt Curtis Jan 06 '17 at 18:59
13

It is possible to restrict git status to the current directory (without child folders) by giving a pathspec using the magic word glob and *::

git status ':(glob)*'
Joël Conraud
  • 141
  • 1
  • 4
5

For me this worked:

git status -uall
sauravjoshi23
  • 837
  • 11
  • 9
4

Imperfect, but this works as well from within the the directory you are interested in:

git status | grep -v ' \.\./'

That will hide all directories that would require an upward reference in their relative path.

If you want to get color spitting out the other end, set color.status to always:

git config color.status always
John Slade
  • 12,246
  • 2
  • 25
  • 20
Marcus Griep
  • 8,156
  • 1
  • 23
  • 24
3

Some plumbing commands do take a directory as parameter:

git ls-files -t -o -m aDirectory

would give you all files changed but not updated (not added to stage), or untracked. And that for a directory.

As written in this thread, git ls-files does not support a '--added option.

more fundamental reason is because ls-files plumbing is about the index.
Added is not about comparison between the index and the work tree.
It is between the HEAD commit and the index, and it does not belong to ls-files plumbing.

So, using commands mentioned here:

git diff-index --name-only -B -R -M -C HEAD src

would give you both non-added and added files

git diff-files --name-only -B -R -M -C src

would give you only non-added files. (while detecting rewrites, renames, copies, ...)

As usual with plumbing commands, some scripting is in order ;)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

When I tried git, I didn't find a way to do that.

I ended up doing:

x@x:~/x$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       b
#       main/a
nothing added to commit but untracked files present (use "git add" to track)

x@x:~/x$ git status | grep main
#       main/a
Macarse
  • 91,829
  • 44
  • 175
  • 230
  • This is the method I was using... the following also gets the status headings: git status |grep "main\|:\$" ... unfortunately you lose colouring. – EoghanM Jun 26 '09 at 13:05
  • Came across this question looking for a generic glob match. This is by far the easiest and works for me, e.g. `git status | grep ext` where ext is the extension. Thanks! –  Jun 16 '17 at 14:48
-2

cd YOUR_REQUIRED_DIRECTORY , then,
git status .

Ratna Halder
  • 1,954
  • 16
  • 17