如何将git状态仅限制在当前目录中的常规文件中?

dan*_*ast 20 git git-status

我想看看当前目录的状态.因为有很多的子目录,改变这一点我希望看到的,下面的命令不会做的伎俩:

git status .
Run Code Online (Sandbox Code Playgroud)

有没有办法获得这种报告,而不是提高产量git status

jub*_*0bs 10

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

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

git status [<options>...] [--] [<pathspec>...]
Run Code Online (Sandbox Code Playgroud)

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

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

Shell脚本

这是一个小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 $?
Run Code Online (Sandbox Code Playgroud)

有关为什么GIT_PREFIX需要恶作剧的更多详细信息,请参阅git别名在错误的目录中操作.

该脚本可在GitHub上的Jubobs/git-aliases中找到.

用它做一个Git别名

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

git config --global alias.statusdot '!sh git-status-dot.sh'
Run Code Online (Sandbox Code Playgroud)

玩具示例

这是一个玩具示例,演示如何使用生成的别名及其功能.

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


gon*_*opp 5

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

find . -maxdepth 1 -type f -print0 | xargs -0 git status
Run Code Online (Sandbox Code Playgroud)

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

find . -maxdepth 1 -type f -print0 | xargs -0 git status nonexistentfile
Run Code Online (Sandbox Code Playgroud)

而且,不,我不打算解决你有一个名为的文件的情况nonexistentfile.