简体   繁体   English

如何只在我提交的文件中执行`git grep`?

[英]How to do `git grep` only in files I have committed?

Is there a way to make git grep only match files that I have committed? 有没有办法使git grep只匹配我提交的文件?

The purpose is to grep in the contents of the file, not the log message. 目的是grep输入文件的内容,而不是日志消息。

author="your name"

git log --pretty="%H" --author="$author" | while read commit_hash; do git show --oneline --name-only $commit_hash | tail -n+2; done | sort | uniq |xargs grep -i "String to grep"

OR shorter version: 或更短的版本:

author="your name"
git log --no-merges --author="$author" --name-only --pretty=format:"" | sort -u |xargs grep -i <string>

Orignal answer used to get the files from a particular user 用于从特定用户获取文件的原始答案

If you want to grep in the diff of a commit, you should probably use git log -G or git log -S : 如果要在提交的差异中进行grep,则可能应使用git log -Ggit log -S

git log -p --author="your name" -G pattern

Both -G and -S will look for the pattern in the diff introduced by a commit, -G-S都会在提交引入的差异中查找模式,
the difference is ( git help log ) : 区别是( git help log ):

To illustrate the difference between -S<regex> --pickaxe-regex and -G<regex> , consider a commit with the following diff in the same file: 为了说明-S<regex> --pickaxe-regex-G<regex>之间的区别,请考虑在同一文件中包含以下差异的提交:

 + return !regexec(regexp, two->ptr, 1, &regmatch, 0); ... - hit = !regexec(regexp, mf2.ptr, 1, &regmatch, 0); 

While git log -G"regexec\\(regexp" will show this commit, git log -S"regexec\\(regexp" --pickaxe-regex will not (because the number of occurrences of that string did not change). 尽管git log -G"regexec\\(regexp"将显示此提交,而git log -S"regexec\\(regexp" --pickaxe-regex则不会)(因为该字符串的出现次数未更改)。

When using -p in conjunction with -G or -S , only the files matching the pattern will be displayed. 当将-p-G-S结合使用时,将仅显示与该模式匹配的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何撤消对Git所做的更改? - How do I undo committed changes on Git? 在某个日期之后,我如何获得* I *已提交给Git(GitHub存储库)的所有文件的列表? - How can I get a list of all files *I* have committed to Git (GitHub repo) after a certain date? 查看我在Git和Mercurial中本地提交的文件 - View files that I have committed locally in Git and Mercurial 如何在 Git 历史记录中 grep(搜索)提交代码 - How to grep (search) committed code in the Git history 每次提交文件时我是否必须&#39;git add&#39;一个文件? - Do I have to 'git add' a file each time it gets committed? 我如何处理我后来在git中忽略的现有文件(已经提交)? - How do I treat the existing files(already committed) that I ignored later in git? 使用 git,我如何忽略一个分支中的文件但将其提交到另一个分支? - Using git, how do I ignore a file in one branch but have it committed in another branch? 如何撤消对Git中某些跟踪文件的已提交和推送的更改? - How do I undo my committed and pushed changes to some of the tracking files in Git? 如何在Git钩子脚本中访问要提交的文件的内容和名称? - How do I access the contents and names of files being committed from within a Git hook script? 提交后忽略Git中的文件 - Ignoring files in Git after they have been committed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM