← 返回命令列表

Linux command

git-grep 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Search for pattern

git grep "[pattern]"

Search with line numbers

git grep -n "[pattern]"

Case insensitive search

git grep -i "[pattern]"

Search in specific commit

git grep "[pattern]" [commit]

Show only filenames

git grep -l "[pattern]"

说明

git grep searches tracked files for patterns, optimized specifically for Git repositories. Unlike regular grep, it ignores untracked files and can search any commit in the repository history. The command is significantly faster on large repositories because it uses Git's index rather than scanning the filesystem directly. It supports the same regex syntax as grep and integrates seamlessly with Git's revision and path specifications.

参数

-n, --line-number
Show line numbers.
-i, --ignore-case
Case insensitive.
-l, --files-with-matches
Show only filenames.
-c, --count
Show match counts.
-w, --word-regexp
Match whole words.
-e _PATTERN_
Pattern argument.
--help
Display help information.

FAQ

What is the git-grep command used for?

git grep searches tracked files for patterns, optimized specifically for Git repositories. Unlike regular grep, it ignores untracked files and can search any commit in the repository history. The command is significantly faster on large repositories because it uses Git's index rather than scanning the filesystem directly. It supports the same regex syntax as grep and integrates seamlessly with Git's revision and path specifications.

How do I run a basic git-grep example?

Run `git grep "[pattern]"` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -n, --line-number do in git-grep?

Show line numbers.