如何将 git status 限制为仅当前目录中的常规文件?

问题描述 投票:0回答:4

我想查看当前目录的状态。因为子目录中有很多更改,而我不想看到这些更改,所以以下命令不起作用: git status .

除了 grep 
git status

的输出之外,有没有办法获得这种报告?

    

git git-status
4个回答
11
投票
git-status -- <pathspec>...

git-status手册页

的概要告诉您可以按路径过滤:
git status [<options>...] [--] [<pathspec>...]

因此,您所要做的就是获取当前目录中常规(非目录)文件对应的路径列表,并将其传递给
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 $?

有关为什么需要
GIT_PREFIX

恶作剧的更多详细信息,请参阅

git别名在错误的目录中运行
该脚本可在 GitHub 上的

jub0bs/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 $



5
投票

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

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

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

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

的文件的情况。

    


4
投票

git status | grep -v /

过滤掉任何包含斜杠(
/

)的路径,这意味着它们位于子目录中。


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

更新:

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


1
投票

除非文件是未跟踪的文件、子模块或忽略的文件。

status

命令的完整文档没有提及将输出限制到当前目录。 但是,您可能需要考虑使用

stash

命令来清理工作树。如果您存储不在当前目录中的所有内容(这需要 --patch 选项仅存储选定部分),则

status
将仅显示您未存储的内容。但是,如果您在工作时多次使用
stash
,那么您应该始终拥有一个干净的工作树,并且可以从一开始就避免此问题。每个工作主体都将打包在一个单独的存储中,等待您准备好提交它。

当然,这对您当前的情况没有多大帮助(除了使用(

stash --patch

)。您可能想尝试的另一种选择是

add
命令的交互模式。然后您可以浏览所有内容更改的文件并有选择地添加您想要的文件(
stash --patch
以相同的方式工作)。
或者,您可以使用其他答案中建议的 grep 解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.