使用Git检查脏索引或未跟踪的文件 [英] Checking for a dirty index or untracked files with Git
问题描述
- 添加到索引但未提交的更改
如何检查我的git存储库中是否有任何未提交的更改: li>
- 未记录的文件
来自脚本?
/ div>
好的时机!我几天前写了一篇关于这个的博客文章,当时我想到了如何将Git状态信息添加到我的提示中。
以下是我的工作:
-
对于脏状态:
#如果当前git分支很脏,则返回*。
函数evil_git_dirty {
[[$(git diff --shortstat 2> / dev / null | tail -n1)!=]]&&回声*
}
-
对于未跟踪的文件(注意
瓷器
标记为git status
,它可以提供很好的可解析输出):
#返回未跟踪文件的数量
函数evil_git_num_untracked_files {
expr`git status --porcelain 2> / dev / null | grep^ ?? | wc -l`
}
尽管 git diff --shortstat
更方便,您还可以使用 git status --porcelain
获取脏文件:
#获取添加到索引的文件数(但未提交)
expr $(git status --porcelain 2> ; / dev / null | grep^ M| wc -l)
#获取未提交和未添加的文件数
expr $(git status --porcelain 2> / dev / null | grep^ M| wc -l)
#获取未获取文件总数
expr $(git status --porcelain 2> / dev / null | egrep ^(M | M)| wc -l)
注意: 2> / dev / null
会过滤掉错误信息,以便您可以在非git目录中使用这些命令。 (它们只会返回 0
作为文件数量。)
编辑:
以下是帖子:
How can I check if I have any uncommitted changes in my git repository:
- Changes added to the index but not committed
- Untracked files
from a script?
git-status
seems to always return zero with git version 1.6.4.2.
Great timing! I wrote a blog post about exactly this a few days ago, when I figured out how to add git status information to my prompt.
Here's what I do:
For dirty status:
# Returns "*" if the current git branch is dirty. function evil_git_dirty { [[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]] && echo "*" }
For untracked files (Notice the
--porcelain
flag togit status
which gives you nice parse-able output):# Returns the number of untracked files function evil_git_num_untracked_files { expr `git status --porcelain 2>/dev/null| grep "^??" | wc -l` }
Although git diff --shortstat
is more convenient, you can also use git status --porcelain
for getting dirty files:
# Get number of files added to the index (but uncommitted)
expr $(git status --porcelain 2>/dev/null| grep "^M" | wc -l)
# Get number of files that are uncommitted and not added
expr $(git status --porcelain 2>/dev/null| grep "^ M" | wc -l)
# Get number of total uncommited files
expr $(git status --porcelain 2>/dev/null| egrep "^(M| M)" | wc -l)
Note: The 2>/dev/null
filters out the error messages so you can use these commands on non-git directories. (They'll simply return 0
for the file counts.)
Edit:
Here are the posts:
Adding Git Status Information to your Terminal Prompt
Improved Git-enabled Shell Prompt
这篇关于使用Git检查脏索引或未跟踪的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!