我怎样才能将git状态限制在当前目录中的常规文件中? [英] How can I restrict git status to regular files in the current directory only?

查看:106
本文介绍了我怎样才能将git状态限制在当前目录中的常规文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  git状态。 

有没有办法获得这种类型的报告, c> git status ?

解决方案

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



git-status 手册页告诉你可以按路径进行过滤:

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

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



有一个问题:因为 git status < pathspec> ... 参数,$ c>报告整个 存储库,您需要检查列表是否为空或者不是。

外壳脚本




$ b

 #!/ bin / sh 

#git-status-dot

#显示工作目录中非目录文件(如果有)的状态

#为了在这个脚本中创建一个名为'status-dot'的Git别名,
#将后者放在您的搜索路径中,使其成为可执行文件并运行

#git config - -global alias.status-dot'! git-status-dot'

#因为GIt别名是从repo的顶层目录运行的,
#我们需要将目录改回到$ GIT_PREFIX。
[$ GIT_PREFIX!=]&& cd$ GIT_PREFIX

#在当前目录中列出非目录文件
lsnondirdot = $(ls -ap | grep -v /)

#如果lsnondirdot不是空的,将它的值传递给git status。
if [-n$ lsnondirdot]
然后
git status - $ lsnondirdot
else
printf工作目录中没有非目录文件\ n
fi

出口$?

有关 GIT_PREFIX shenanigans是必需的,请参阅 git别名在错误的目录中运行。 p>

该脚本位于 Jubobs / git-aliases 在GitHub上。



创建一个Git别名



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

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



玩具示例



这是一个玩具示例,演示如何使用结果化的别名以及它的作用。


$ b

 #initialize回购
$ mkdir testgit
$ cd testgit
$ git init

#创建两个文件
$ mkdir foo
$ touch foo / foo.txt
$ touch bar.txt

#旧的git状态报告subdir / test1.txt未被跟踪...
$ git status
在分支上master

初始提交

未被跟踪的文件:
(使用git add< file> ...来包含将被提交的内容)

bar.txt
foo /

没有添加提交,但未跟踪的文件存在(使用git add跟踪)
#...而我们的新别名,git status-dot,只关心当前目录中常规文件的
# ectory
$ git status-dot
在分支master上

初始提交

未经记录的文件:
(使用git add< file> ; ...包含在将要提交的内容中)

bar.txt

没有添加提交,但未跟踪的文件存在(使用git add来跟踪)

#如果我们删除README.md ...
$ rm README.md
#...好的旧git状态仍然会打扰我们/ subdir ...
$ git status
初始提交

未被跟踪的文件:
(使用git add< file> ...来包含将要提交的内容)

foo /

没有添加提交,但未跟踪的文件存在(使用git add跟踪)
#...而git statusdot不
$ git status-dot
在工作目录中没有非目录文件
$


I would like to see the status of the current directory. Because there are lots of changes in sub-directories, which I do not want to see, the following command doesn't do the trick:

git status .

Is there any way of getting this kind of report, short of grepping the output of git status?

解决方案

Use git-status -- <pathspec>...

The synopsis of the git-status man page tells you that you can filter by paths:

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

Therefore, all you have to do is get a list of paths corresponding to the regular (non-directory) files in the current directory, and pass that to git-status.

There is one gotcha: because git status reports about the whole repository if passed an empty <pathspec>... argument, you need to check whether the list is empty or not.

Shell script

Here is a small shell script that does what you want.

#!/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 $?

For more details about why the GIT_PREFIX shenanigans are required, see git aliases operate in the wrong directory.

The script is available at Jubobs/git-aliases on GitHub.

Make a Git alias out of it

For convenience, you can create a Git alias that calls the script; make sure the script is on your path, though.

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

Toy example

Here is a toy example demonstrating how to use the resulting alias and what it does.

# 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
$

这篇关于我怎样才能将git状态限制在当前目录中的常规文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆