如果我们想在版本化的源代码中搜索,那么使用git grep比普通grep好吗?

时间:2023-02-04 14:04:50

In a git repository, is there any difference/benefit using git grep over good old grep?
An example would be?

在git存储库中,使用git grep与使用老grep有什么不同/好处吗?一个例子将是?

4 个解决方案

#1


28  

The two are very similar. The main difference is that git grep defaults to searching in the files that are tracked by git.

两者非常相似。主要的区别是git grep默认在git跟踪的文件中进行搜索。

Examples

If I want to find foo within my project I can use git grep or good ol' stand-alone grep:

如果我想在我的项目中找到foo,我可以使用git grep或good ol' independent grep:

git grep foo
grep -R foo .

The git grep version will only search in files tracked by git, whereas the grep version will search everything in the directory. So far so similar; either one could be better depending on what you want to achieve.

git grep版本将只在git跟踪的文件中搜索,而grep版本将搜索目录中的所有内容。到目前为止,类似;任何一个都可以更好地取决于你想要达到的目标。

What if we want to limit the search to only .rb files?

如果我们想要将搜索限制为.rb文件呢?

git grep foo -- *.rb
grep -R --include=*.rb foo .

The plain old grep version is getting a bit more wordy, but if you're used to using grep that may not be a problem. They're still not going to search exactly the same files, but again it depends on what you want to achieve.

普通的旧grep版本变得有点啰嗦,但是如果您习惯使用grep,这可能不是问题。他们仍然不会搜索完全相同的文件,但这又取决于你想要实现什么。

What about searching in the previous version of the project?

在项目的前一个版本中搜索怎么样?

git grep foo HEAD^
git checkout HEAD^; grep -R foo .; git checkout -

This is where git grep makes a real difference: You can search in another revision of the project without checking it out first. This isn't a situation that comes up too often for me though; I usually want to search in the version of the project I have checked out.

这就是git grep的真正意义所在:您可以在项目的另一个版本中搜索,而不必先检查它。这不是我经常遇到的情况;我通常想在我签过的项目的版本中搜索。

Configuring git grep

There are some git config variables that modify the behaviour of git grep and avoid the need to pass a couple of command line arguments:

有一些git配置变量可以修改git grep的行为,并避免传递一些命令行参数:

  • grep.lineNumber: Always show line numbers of matches (you can pass -n to both grep and git grep to get this behaviour)
  • grep。lineNumber:始终显示匹配的行号(您可以将-n传递给grep和git grep以获得这种行为)
  • grep.extendedRegexp: Always use extended regular expressions (you can pass -E to both grep and git grep to get this behaviour)
  • grep。extendedRegexp:总是使用扩展的正则表达式(您可以将-E传递给grep和git grep以获得这种行为)

In practice

In practice I have gg aliased to git grep -En, and this almost always does what I want.

在实践中,我已经添加了git grep -En,这几乎总是我想要的。

#2


11  

The main advantage of git grep is that it can find the patterns in the git repository, i. e. also in others than the current version of the source. This cannot be done using the standard grep of course. Also there are a lot more features in the git grep like pattern arithmetic (things like git grep -e pattern1 --and --not \( -e pattern2 -e pattern3 \)), tree search using glob (things like git grep pattern -- '*.[ch]' to search only in .c and .h files) and some more.

git grep的主要优点是它可以在git存储库中找到模式,也就是说,它可以在其他版本中找到模式,而不是源的当前版本。当然,这不能使用标准grep实现。此外,git grep中还有很多特性,比如模式算法(比如git grep -e pattern1——而不是\(-e pattern2 -e pattern3 \)),使用glob进行树搜索(比如git grep pattern - '*)。“只在。c和。h文件中搜索)和更多。

Here's an example session for searching in an older revision:

这里有一个在旧版本中搜索的示例会话:

$ mkdir git-test                 # create fresh repository
$ cd git-test/
$ git init .
Initialized empty Git repository in /home/alfe/git-test/.git/
$ echo eins zwei drei > bla      # create example file
$ git add bla                    # add and commit it
$ git commit bla
[master (root-commit) 7494515] .
 1 file changed, 1 insertion(+)
 create mode 100644 bla
$ echo vier fuenf sechs > bla    # perform a change on that file
$ git commit -m 'increase' bla   # commit it
[master 062488e] increase
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git grep eins | cat            # grep for outdated pattern in current version
                                  # (finds nothing)
$ git grep eins master^ | cat    # grep for outdated pattern on former version
                                  # finds it:
master^:bla:eins zwei drei

#3


2  

If you're searching for patterns/strings within a git repository (i.e. in files that are already tracked), then yes, git grep should be much faster typically than regular grep as it is indexed. (You can try this out manually, the git-grep should be perceptibly faster)

如果您正在git存储库中搜索模式/字符串(例如在已经被跟踪的文件中),那么是的,git grep通常比常规grep要快得多,因为它是被索引的。(您可以手动尝试一下,git-grep应该更快)

#4


1  

git grep only searches in the tracked files in the repo.

git grep只在repo中的跟踪文件中搜索。

With grep you have to pass the list of files to search through and you would have filter out any untracked files yourself.

使用grep,您必须传递要搜索的文件列表,并且您必须自己过滤掉任何未跟踪的文件。

So if you are searching for something that you know is in the repo, git grep saves you time as all you have to do is provide the pattern. It also is useful for not having to search through anything that is untracked in the repo.

因此,如果您正在搜索某个在repo中知道的内容,git grep将节省您的时间,因为您所要做的就是提供模式。它对于不必搜索回购中未跟踪的任何内容也很有用。

#1


28  

The two are very similar. The main difference is that git grep defaults to searching in the files that are tracked by git.

两者非常相似。主要的区别是git grep默认在git跟踪的文件中进行搜索。

Examples

If I want to find foo within my project I can use git grep or good ol' stand-alone grep:

如果我想在我的项目中找到foo,我可以使用git grep或good ol' independent grep:

git grep foo
grep -R foo .

The git grep version will only search in files tracked by git, whereas the grep version will search everything in the directory. So far so similar; either one could be better depending on what you want to achieve.

git grep版本将只在git跟踪的文件中搜索,而grep版本将搜索目录中的所有内容。到目前为止,类似;任何一个都可以更好地取决于你想要达到的目标。

What if we want to limit the search to only .rb files?

如果我们想要将搜索限制为.rb文件呢?

git grep foo -- *.rb
grep -R --include=*.rb foo .

The plain old grep version is getting a bit more wordy, but if you're used to using grep that may not be a problem. They're still not going to search exactly the same files, but again it depends on what you want to achieve.

普通的旧grep版本变得有点啰嗦,但是如果您习惯使用grep,这可能不是问题。他们仍然不会搜索完全相同的文件,但这又取决于你想要实现什么。

What about searching in the previous version of the project?

在项目的前一个版本中搜索怎么样?

git grep foo HEAD^
git checkout HEAD^; grep -R foo .; git checkout -

This is where git grep makes a real difference: You can search in another revision of the project without checking it out first. This isn't a situation that comes up too often for me though; I usually want to search in the version of the project I have checked out.

这就是git grep的真正意义所在:您可以在项目的另一个版本中搜索,而不必先检查它。这不是我经常遇到的情况;我通常想在我签过的项目的版本中搜索。

Configuring git grep

There are some git config variables that modify the behaviour of git grep and avoid the need to pass a couple of command line arguments:

有一些git配置变量可以修改git grep的行为,并避免传递一些命令行参数:

  • grep.lineNumber: Always show line numbers of matches (you can pass -n to both grep and git grep to get this behaviour)
  • grep。lineNumber:始终显示匹配的行号(您可以将-n传递给grep和git grep以获得这种行为)
  • grep.extendedRegexp: Always use extended regular expressions (you can pass -E to both grep and git grep to get this behaviour)
  • grep。extendedRegexp:总是使用扩展的正则表达式(您可以将-E传递给grep和git grep以获得这种行为)

In practice

In practice I have gg aliased to git grep -En, and this almost always does what I want.

在实践中,我已经添加了git grep -En,这几乎总是我想要的。

#2


11  

The main advantage of git grep is that it can find the patterns in the git repository, i. e. also in others than the current version of the source. This cannot be done using the standard grep of course. Also there are a lot more features in the git grep like pattern arithmetic (things like git grep -e pattern1 --and --not \( -e pattern2 -e pattern3 \)), tree search using glob (things like git grep pattern -- '*.[ch]' to search only in .c and .h files) and some more.

git grep的主要优点是它可以在git存储库中找到模式,也就是说,它可以在其他版本中找到模式,而不是源的当前版本。当然,这不能使用标准grep实现。此外,git grep中还有很多特性,比如模式算法(比如git grep -e pattern1——而不是\(-e pattern2 -e pattern3 \)),使用glob进行树搜索(比如git grep pattern - '*)。“只在。c和。h文件中搜索)和更多。

Here's an example session for searching in an older revision:

这里有一个在旧版本中搜索的示例会话:

$ mkdir git-test                 # create fresh repository
$ cd git-test/
$ git init .
Initialized empty Git repository in /home/alfe/git-test/.git/
$ echo eins zwei drei > bla      # create example file
$ git add bla                    # add and commit it
$ git commit bla
[master (root-commit) 7494515] .
 1 file changed, 1 insertion(+)
 create mode 100644 bla
$ echo vier fuenf sechs > bla    # perform a change on that file
$ git commit -m 'increase' bla   # commit it
[master 062488e] increase
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git grep eins | cat            # grep for outdated pattern in current version
                                  # (finds nothing)
$ git grep eins master^ | cat    # grep for outdated pattern on former version
                                  # finds it:
master^:bla:eins zwei drei

#3


2  

If you're searching for patterns/strings within a git repository (i.e. in files that are already tracked), then yes, git grep should be much faster typically than regular grep as it is indexed. (You can try this out manually, the git-grep should be perceptibly faster)

如果您正在git存储库中搜索模式/字符串(例如在已经被跟踪的文件中),那么是的,git grep通常比常规grep要快得多,因为它是被索引的。(您可以手动尝试一下,git-grep应该更快)

#4


1  

git grep only searches in the tracked files in the repo.

git grep只在repo中的跟踪文件中搜索。

With grep you have to pass the list of files to search through and you would have filter out any untracked files yourself.

使用grep,您必须传递要搜索的文件列表,并且您必须自己过滤掉任何未跟踪的文件。

So if you are searching for something that you know is in the repo, git grep saves you time as all you have to do is provide the pattern. It also is useful for not having to search through anything that is untracked in the repo.

因此,如果您正在搜索某个在repo中知道的内容,git grep将节省您的时间,因为您所要做的就是提供模式。它对于不必搜索回购中未跟踪的任何内容也很有用。