Git搜索

git grep

命令说明示例
git grep <pattern>最基础用法:在当前目录及子目录的已跟踪文件中搜索模式。git grep "function myFunc"
git grep -n <pattern>显示行号:在匹配的行前显示其在文件中的行号。git grep -n "TODO"
git grep -i <pattern>忽略大小写:进行不区分大小写的搜索。git grep -i "error"
git grep -l <pattern>仅显示文件名:只列出包含匹配项的文件名,不显示具体内容。git grep -l "const API_KEY"
git grep -w <pattern>整词匹配:确保模式匹配完整的单词(例如,搜索 "map" 不会匹配 "mapping")。git grep -w "catch"
git grep -C <num> <pattern>显示上下文:显示匹配行及其前后各 <num> 行的内容(例如 -C 2)。git grep -C 2 "Exception"
git grep -e <pattern>使用多个模式:搜索多个模式中的任意一个。可以多次使用 -egit grep -e "import" -e "require"
git grep -E <pattern>使用扩展正则:将模式解释为扩展正则表达式(支持 +, `()` 等)。
git grep -F <pattern>固定字符串:将模式视为普通字符串,而非正则表达式,搜索速度更快。git grep -F "https://example.com"
git grep -c <pattern>统计匹配次数:不显示匹配内容,只显示每个文件中的匹配数量。git grep -c "deprecated"
git grep <pattern> -- <pathspec>限定路径:只在指定的文件或目录中搜索。-- 用于分隔模式和路径,防止路径被误认为模式。git grep "class User" -- src/models/
git grep -L <pattern>列出不匹配的文件:显示不包含任何匹配项的文件列表。git grep -L "test"
git grep -m <num> <pattern>限制匹配数量:每个文件最多显示 <num> 个匹配项。git grep -m 5 "warning"
git grep --cached <pattern>仅搜索暂存区:只在已添加到暂存区(已跟踪) 的文件中搜索。git grep --cached "console.log"
git grep --untracked <pattern>搜索未跟踪文件:也在未被 Git 跟踪的文件中搜索(如新建的文件)。git grep --untracked "newFeature"
git grep -p <pattern>显示函数上下文:在显示匹配行的同时,显示其所属的函数或方法名(对于代码导航很有用)。git grep -p "calculateTotal"
git grep -B <num> <pattern>显示前文上下文:显示匹配行及其之前的 <num> 行(例如 -B 3)。git grep -B 3 "fatal error"
git grep -A <num> <pattern>显示后文上下文:显示匹配行及其之后的 <num> 行(例如 -A 3)。git grep -A 3 "return false;"
git grep -o <pattern>仅显示匹配部分:只输出行中与模式匹配的部分,而不是整行内容。git grep -o "\\<[a-zA-Z_]\\w*\\>" (匹配所有单词)
git grep --color=always <pattern> | less -R带颜色分页查看:强制高亮显示匹配项,并通过 less 分页器浏览(-R 参数让 less 能解析颜色代码)。git grep --color=always "bug" | less -R
git grep -f <file> <pathspec>从文件读取模式:从文件 <file> 中每行读取一个模式进行搜索。git grep -f patterns.txt -- src/
git grep --and -e <pat1> -e <pat2>逻辑与(AND):查找同时包含 <pat1>  <pat2> 的行。git grep --and -e "user" -e "login"
git grep --or -e <pat1> -e <pat2>逻辑或(OR):查找包含 <pat1>  <pat2> 的行(同多个 -e)。git grep --or -e "error" -e "exception"
git grep --not -e <pat1> <pat2>逻辑非(NOT):查找包含 <pat2> 但包含 <pat1> 的行。git grep --not -e "test" "production"
git grep --recurse-submodules搜索子模块:在搜索时同时递归进入所有子模块。git grep --recurse-submodules "API_ENDPOINT"

常用模式

# 忽略大小写,显示行号,整词匹配
git grep -inw mystring

# 指定提交/分支,指定文件
git grep -inw mystring 提交哈希简写/分支名 *.c

git log

# 查看某个修改何时被引入
git log -S func_xx

# 查看某个功能的修改历史
git log -L:func_xx:functions.c

# 图形化查看所有提交历史,每个提交一行
git log --oneline --graph --all

# 查看最近2次提交
git log -2

# 查看dev分支的提交
git log dev
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇
error: Content is protected !!