使用 Git 检查脏索引或未跟踪的文件 [英] Checking for a dirty index or untracked files with Git

查看:29
本文介绍了使用 Git 检查脏索引或未跟踪的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查我的 git 存储库中是否有任何未提交的更改:

How can I check if I have any uncommitted changes in my git repository:

  1. 已添加到索引但未提交的更改
  2. 未跟踪的文件

来自脚本?

git-status 似乎总是在 git 版本 1.6.4.2 中返回零.

git-status seems to always return zero with git version 1.6.4.2.

推荐答案

好时机!几天前我写了一篇关于这个的博客文章,当时我想出了如何将 git 状态信息添加到我的提示中.

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.

这就是我所做的:

  1. 对于脏状态:

  1. For dirty status:

# Returns "*" if the current git branch is dirty.
function evil_git_dirty {
  [[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]] && echo "*"
}

  • 对于未跟踪的文件(注意 --porcelain 标志到 git status,它为您提供了很好的可解析输出):

  • For untracked files (Notice the --porcelain flag to git 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` 
    }
    

  • 虽然git diff --shortstat更方便,但你也可以使用git status --porcelain来获取脏文件:

    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)
    

    注意:2>/dev/null 过滤掉错误信息,这样你就可以在非 git 目录上使用这些命令.(他们将简单地返回 0 作为文件计数.)

    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.)

    编辑:

    这里是帖子:

    将 Git 状态信息添加到终端提示

    改进的支持 Git 的 Shell 提示

    这篇关于使用 Git 检查脏索引或未跟踪的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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