Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get git-status of a single folder in a non-recursive way (not showing its subfolders)

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.

like image 859
Yuval Atzmon Avatar asked Oct 18 '25 15:10

Yuval Atzmon


1 Answers

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