如何获得单个子文件夹的git-status?

Eog*_*anM 76 git

当我在我的存储库的子文件夹中执行git状态时,它还包括父文件夹的状态.

有没有办法将git-status限制在一个特定的文件夹中?

Mat*_*tis 92

git status .
Run Code Online (Sandbox Code Playgroud)

将显示当前目录和子目录的状态.

例如,此树中的给定文件(数字):

a/1
a/2
b/3
b/4
b/c/5
b/c/6
Run Code Online (Sandbox Code Playgroud)

从子目录"b",git status显示整个树中的新文件:

% 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
#
Run Code Online (Sandbox Code Playgroud)

git status .只是在"b"及以下显示文件.

% 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
#
Run Code Online (Sandbox Code Playgroud)

只是这个子目录,而不是下面

git status .以递归方式显示"b"以下的所有文件.要仅显示"b"中的文件而不是下面的文件,您需要将文件列表(而不是目录)传递给git status.这有点繁琐,取决于你的shell.

岩组

在zsh中,您可以使用"glob限定符"选择普通文件(.).例如:

% git status *(.)
On branch master

Initial commit

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

        new file:   3
        new file:   4
Run Code Online (Sandbox Code Playgroud)

巴什

Bash没有glob限定符,但是你可以使用GNU find来选择普通文件,然后将它们传递给git status像:

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
Run Code Online (Sandbox Code Playgroud)

这使用-maxdepthGNU查找扩展.POSIX查找没有-maxdepth,但你可以这样做:

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
Run Code Online (Sandbox Code Playgroud)

  • 抱歉花了这么长时间回到这个答案 - 简单而正确!我想知道在我的问题之后是否将<pathspec>添加到git状态? (2认同)

sau*_*i23 7

对我来说这有效:

git status -uall
Run Code Online (Sandbox Code Playgroud)


Joë*_*aud 5

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)*'
Run Code Online (Sandbox Code Playgroud)

  • 这太棒了!正是我正在寻找的东西! (4认同)