Git提交某个单词的差异或内容

Git提交某个单词的差异或内容

本文介绍了如何grep Git提交某个单词的差异或内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Git代码仓库中,我想列出包含某个单词的所有提交。我试过这个

git log -p | grep --context = 4word

但它并不一定会给我回文件名(除非它远不及我搜索的单词5
行,我也试过了

  git grepword

但它只给出了现在的文件而不是历史记录。



如何搜索整个历史记录,以便跟踪特定单词的变化?我的意思是搜索我的代码库以查找单词的发生情况以追踪更改(在文件历史记录中搜索)。

解决方案

如果您想查找提交消息包含给定单词的所有提交,请使用

  $ git log --grep = word 

如果您想查找所有在文件内容中添加或删除word的提交(更确切地说:word出现次数发生变化) ,即搜索提交内容,使用使用 $ b

  $ git log -Sword 

在现代的git中也有

$ p $ $ git log -Gword

查找差异,其添加或删除的行匹配word(也 )。



请注意,默认情况下 -G 接受一个正则表达式,而 -S 接受一个字符串,但可以修改为使用 - pickaxe-regex 来接受正则表达式。


In a Git code repository I want to list all commits that contain a certain word. I tried this

git log -p | grep --context=4 "word"

but it does not necessarily give me back the filename (unless it's less that 5lines away from the word I searched for. I also tried

git grep "word"

but it gives me only present files and not the history.

How do I search the entire history so I can follow changes on a particular word? I mean to search my codebase for occurrences of word to track down changes (search in files history).

解决方案

If you want to find all commits where commit message contains given word, use

$ git log --grep=word

If you want to find all commits where "word" was added or removed in the file contents (to be more exact: where number of occurences of "word" changed), i.e. search the commit contents, use so called 'pickaxe' search with

$ git log -Sword

In modern git there is also

$ git log -Gword

to look for differences whose added or removed line matches "word" (also commit contents).

Note that -G by default accepts a regex, while -S accepts a string, but can be modified to accept regexes using the --pickaxe-regex.

这篇关于如何grep Git提交某个单词的差异或内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:42