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


81

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

有没有一种方法可以将git-status约束到特定文件夹?


1
不,“ git status”。给出同级文件夹,例如“ ../sibling”
EoghanM 2009年

您是否愿意提供更多有关为什么要过滤掉树中其他位置的更改的上下文?您想在什么情况下使用此功能?
格雷格·培根,2009年

1
如果正常的“ git status”命令的输出值超过了全屏显示的值,则
-EoghanM

Answers:


100
git status .

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

例如,此树中的给定文件(编号):

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

从子目录“ 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
#

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
#

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

git status .递归显示“ b”下的所有文件。要仅在“ b”中显示文件,而不在下方显示,您需要将仅文件列表(而不是目录)传递给git status。这有点奇怪,具体取决于您的外壳。

sh

在zsh中,您可以使用“ glob qualifier”选择普通文件(.)。例如:

% 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没有glob限定符,但是您可以使用GNUfind选择普通文件,然后将它们传递给git statuslike:

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

这使用-maxdepthGNU find扩展。POSIX find没有-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

2
很抱歉花了很长时间才回到这个答案-简单正确!我想知道是否在我的问题之后将<pathspec>添加到git status?
EoghanM 2012年

如果您仅对.引用的当前文件夹感兴趣,而不对其子文件夹感兴趣,该怎么办。有什么解决办法吗?谢谢!
benjaminz

@benjaminz取决于您的使用-诸如此类的事情,我使用外壳遍历来选择文件,但不选择子目录,例如(对于zsh)git status *(.)。Bash并不具有仅遍历文件的捷径,但是您可以使用扩展的findeg find . -type f -maxdepth 1 -exec git status {} +。似乎是您想要放入别名或脚本而不是一直输入的那种东西。
马特·柯蒂斯

3

一些管道命令确实将目录作为参数:

git ls-files -t -o -m aDirectory

会为您提供所有已更改但未更新(未添加到阶段)或未跟踪的文件。还有一个目录。

该线程中所述,git ls-files不支持'--added选项。

更根本的原因是因为ls-files管道与指数有关
添加的内容不是关于索引和工作树之间的比较。
它在HEAD提交和索引之间,并且不属于ls文件管道。

因此,使用此处提到的命令:

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

会给你既未添加文件又添加了文件

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

只会给您未添加的文件。(在检测重写,重命名,复制等时)

与管道命令一样,按顺序进行一些脚本编写;)


3

不完美,但是在您感兴趣的目录中也可以正常工作:

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

这将隐藏所有需要在其相对路径中向上引用的目录。

如果要使另一端喷出颜色,请设置color.statusalways

git config color.status always

2
我喜欢grep如何始终提供一个临时解决方案,如果您不知道更具体的解决方案。为taco-bell编程加油。
murftown 2011年

这是更强大的方法!
benjaminz

2

git status通过使用魔术词glob和给出路径说明,可以限制当前目录(没有子文件夹)*:

git status ':(glob)*'

这很棒!正是我要寻找的!
poindexter

1

当我尝试git时,我没有找到一种方法来做。

我最终做了:

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

这是我使用的方法...以下内容也获得了状态标题:git status | grep“ main \ |:\ $” ...不幸的是,您失去了颜色。
EoghanM 2009年

遇到了这个问题,寻找通用的全局匹配。到目前为止,这是最简单的方法,并且对我git status | grep ext有用,例如ext是扩展名。谢谢!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.