20

我想查看当前目录的状态。因为子目录中有很多我不想看到的变化所以下面的命令不能解决问题:

git status .

有没有办法得到这种报告,除了 grepping 的输出git status

4

4 回答 4

10

利用git-status -- <pathspec>...

git-status手册页的概要告诉您可以按路径过滤:

git status [<options>...] [--] [<pathspec>...]

因此,您所要做的就是获取与当前目录中的常规(非目录)文件对应的路径列表,并将其传递给git-status.

有一个问题:因为如果传递一个空参数会git status报告整个<pathspec>...存储库,您需要检查列表是否为空。

外壳脚本

这是一个小shell脚本,可以满足您的需求。

#!/bin/sh

# git-status-dot
#
# Show the status of non-directory files (if any) in the working directory
#
# To make a Git alias called 'status-dot' out of this script,
# put the latter on your search path, make it executable, and run
#
#   git config --global alias.status-dot '! git-status-dot'

# Because GIt aliases are run from the top-level directory of the repo,
# we need to change directory back to $GIT_PREFIX.
[ "$GIT_PREFIX" != "" ] && cd "$GIT_PREFIX"

# List Non-Directory Files in the Current Directory
lsnondirdot=$(ls -ap | grep -v /)

# If "lsnondirdot" is not empty, pass its value to "git status".
if [ -n "$lsnondirdot" ]
then
    git status -- $lsnondirdot
else
    printf "No non-directory files in the working directory\n"
fi

exit $?

有关为什么GIT_PREFIX需要恶作剧的更多详细信息,请参阅git aliases operation in the wrong directory

该脚本可在GitHub 上的Jubobs/git-aliases获得。

用它创建一个 Git 别名

为方便起见,您可以创建一个调用脚本的 Git 别名;不过,请确保脚本在您的路径上。

git config --global alias.statusdot '!sh git-status-dot.sh'

玩具示例

这是一个演示如何使用生成的别名及其作用的玩具示例。

# initialize a repo
$ mkdir testgit
$ cd testgit
$ git init

# create two files
$ mkdir foo
$ touch foo/foo.txt
$ touch bar.txt

# good old git status reports that subdir/test1.txt is untracked... 
$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    bar.txt
    foo/

nothing added to commit but untracked files present (use "git add" to track)
# ... whereas our new alias, git status-dot, only cares
# about regular files in the current directory
$ git status-dot
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    bar.txt

nothing added to commit but untracked files present (use "git add" to track)

# and if we delete README.md ...
$ rm README.md
# ... good old git status still bother us about /subdir ...
$ git status
Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    foo/

nothing added to commit but untracked files present (use "git add" to track)
# ... whereas git statusdot doesn't
$ git status-dot
No non-directory files in the working directory
$
于 2015-03-18T14:25:31.313 回答
5

也许有更合适的方法可以做到这一点,但你可以简单地

find . -maxdepth 1 -type f -print0 | xargs -0 git status

当然,如果当前目录中没有常规文件,这将失败。在这种情况下,您可以使用额外丑陋的版本

find . -maxdepth 1 -type f -print0 | xargs -0 git status nonexistentfile

而且,不,我不会解决您有一个名为nonexistentfile.

于 2015-03-18T14:17:36.133 回答
4

您可以使用:

git status | grep -v /

过滤掉任何包含斜杠 ( /) 的路径,这意味着它们位于子目录中。

您还将失去更改路径的红色/绿色。

更新:

此解决方案不显示从当前目录移动或移动到当前目录的文件,因为移动操作的源和目标显示在输出的同一行上。但是会显示在当前目录中重命名的文件(没有在目录之间移动)。

于 2015-03-18T14:40:33.417 回答
1

我相信答案是否定的……

除非文件是未跟踪的文件、子模块或被忽略的文件。status命令的完整文档没有提到将输出限制到当前目录。

但是,您可能需要考虑使用stash命令来清理工作树。如果您存储不在当前目录中的所有内容(这需要--patch仅存储选择性部分的选项),那么status只会显示您没有存储的内容。但是,如果您stash在工作时多次使用,那么您应该始终拥有一个干净的工作树,并且您可以从一开始就避免这个问题。每个工作主体都将打包在一个单独的存储区中,等待您准备好提交它。

当然,这对您当前的情况没有多大帮助(除了使用 ( stash --patch)。您可能想尝试的另一种选择是命令的交互模式add。然后您可以浏览所有更改的文件并有选择地添加您需要的文件想要(stash --patch工作方式相同)。

或者,您可以使用其他答案中建议的 grep 解决方案。

于 2015-03-18T14:43:42.230 回答