在提交之前如何通过您的staged文件进行grep? [英] How grep through your staged files prior to committing?

查看:97
本文介绍了在提交之前如何通过您的staged文件进行grep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在运行 git commit 之前,我经常运行以下命令:

  git grep --cached -l -I调试器

我认为它与:

  git diff --cached 

(它会告诉你所有你将要提交的改变,例如,将会显示你在分阶段文件中的差异)。

不幸的是,我发现 --cached 选项对于 git grep 只是简单地告诉git仅一切都在它的索引中。

那么我该如何运行 git grep 并让它只通过我的staged文件进行grep?



(是的,我知道我可以简单地执行 git diff --cached 并在其中搜索,但我会而是具有通过我的staged文件grep的编程能力。)

解决方案

很多预提交钩子使用 git diff-index --cached -S< pat> REV 来查找添加或删除特定模式的更改。所以在你的情况下, git diff-index --cached -Sdebugger HEAD 。您可能想要添加 -u 以获得差异,否则它只是识别违规文件。


So prior to running git commit I often will run the following:

git grep --cached -l -I "debugger"

I thought it was similar to:

git diff --cached

(which will show you all the changes you are about to commit, ie. will show you the diff in your staged files).

Unfortunately, I just found that the --cached option for git grep simply tells git to "only" look at everything in its index.

So how can I run git grep and have it only grep through my staged files?

(Yes, I know I could simply do git diff --cached and search in that, but I would rather have the programmatic ability to grep through my staged files.)

解决方案

A lot of pre-commit hooks use git diff-index --cached -S<pat> REV to find changes which add or remove a particular pattern. So in your case, git diff-index --cached -Sdebugger HEAD. You may want to add -u to get a diff as well, otherwise it just identifies the offending file.

这篇关于在提交之前如何通过您的staged文件进行grep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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