3

How can I get git-status of a single folder in a non-recursive way?

This is not a duplicate of this question since they address there how to git-status a single folder and it subdirectories, e.g. the accepted answer there is to use git status . which returns a recursive result.

Community
  • 1
  • 1
Yuval Atzmon
  • 5,645
  • 3
  • 41
  • 74

2 Answers2

1

go into the desired folder and the use:

git status .

This will display the status of the given folder which you are in right now.

Another option is to use:

git ls-files -t -o -m <desired path>

This would display all files changed but not updated (unstaged), or untracked for the given directory.


Non recursive way:

In the desired folder use the combination of git status + grep to filter the results

git status | grep -v /
Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 1
    This answer ignores my question.. I asked how to get `git-status` **in a non-recursive way** – Yuval Atzmon Dec 06 '15 at 05:37
  • Thanks, should it be `git status | grep -v /` or `git status . | grep -v /`? – Yuval Atzmon Dec 09 '15 at 19:41
  • Each one doe something little bit different. read the description next to each one – CodeWizard Dec 10 '15 at 06:12
  • This will not include files moved out of the chosen folder. This is displayed by the git as a rename and the new location will have a `/` – Annan Yearian Nov 09 '22 at 09:54
  • Alh of those are wrong. The first one shows the status for changes, but subdirectories too (I tested this), the second one shows the status of all files, for subdirectories too, and the third one only works when you are currently in that directory, instead of giving the subdir as a parameter to git. – Evi1M4chine Apr 24 '23 at 12:09
0

The solution is in the definition of pathspec (see man pathspec). It allows non-recursing glob patters and even exclusion patterns and attribute filtering.

In essence: Special paths can be given, that start with a :, and some ”magic signature” before the actual path. There are long and a short form signatures.

For your case, the following works nicely:

git status ':(glob)mypath/*'

This is the long form, :(*)…, where * is a keyword. (glob in this case.) There is no short one for glob.

Note the single quotes, to make git itself glob, and not your shell. (Which may otherwise lead to passing a huge number of arguments to git, if your directory of choice has lot of entries.)

You can also specifically exclude subdirectory contents, by writing:

git status ':^mypath/*/*'

Here we can use the short form :?…, where ? is a key character. (^ in this case.) The long form of :^ is :(exclude).

This variant may have a slightly different meaning. (I have not verified this, but it probably includes changes to the actual subdirectories themselves but not changes to the files inside them.)

Evi1M4chine
  • 6,992
  • 1
  • 24
  • 18