假設我在 master 分支中,我只想從某些提交中 grep 單詞,但這些單詞仍在 master 分支中。換句話說,我想找到在某些提交中添加的那些詞并且仍然存在。
前任,
commit 76894a679551e1c9e98593219033a2cf115b163d (HEAD -> master)
Change to fruit
-Apple apple dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
Banana apple dolor apple amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
commit e3e0a64cf46cbe49eea17205f679e98ca213a50b
Change to apple
-Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
Apple apple dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
commit 71b609da3fe64092dd598a69e36770eb5636afec
Init
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
(END)
我想找出在 76894 中添加的蘋果,所以只有參考的才是我要找的,
Banana "apple" dolor apple amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.而不是Banana apple dolor "apple" amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
uj5u.com熱心網友回復:
- 如果要在特定提交的任何檔案中搜索單詞,可以使用:
git grep -e "word" <sha>
如果您想在一系列提交上執行此操作,則必須獲取此范圍的所有哈希值,并遍歷該范圍:
# for example, using a 'while' loop in bash :
git rev-list a..b | while read sha; do git grep -e "word" $sha; done
- 如果您只想在特定提交的差異中發現該詞,請使用以下鎬選項之一
git log:
git log -Sword a..b
# or
git log -Gword a..b
uj5u.com熱心網友回復:
我認為你需要的只是一個簡單的git grep:
git grep apple master
例如。該git grep命令采用可選的樹引數,“樹”是作為快照存盤在提交中的內容。
您特別要求:
在某些提交中添加的單詞仍然存在
因為 Git 存盤快照,而不是差異,所以在任何先前提交中添加并仍然存在的單詞,根據定義,在快照中。
另一方面,如果您的意思是例如:
- 如果出現以下情況,我想找到“蘋果”:
- 它存在于提交 C1、C2 或 C3 的檔案F中,并且
- 現在存在的尖端
master檔案?F以及
那么你需要一個兩遍演算法:首先,檢查正則運算式是否在非提示提交中的任何特定檔案中匹配,然后在提示提交中再次檢查。當且僅當您在第 1 步和第 2 步中都找到此提示時,才列印提示提交的哈希 ID(可能還有檔案和匹配行)。
請注意,一次git grep可以搜索多個提交。要查看該詞是否apple出現在這三個提交中的任何檔案中,只需運行:
git grep apple C1 C2 C3
輸出將包含原始提交哈希 ID,然后是檔案名,然后是匹配的行:
$ git grep "go build" $(git rev-list HEAD)
f6b272b0c674c2d5022e90c3dec868af4ea26522:Makefile: go build -ldflags "-X main.version=$$(git describe --always --dirty)"
e50f217c3e11c6ee1fb67ee720c97f4085d6c46b:Makefile: go build
(這個特定的存盤庫現在非常小,只有Makefile我-version在命令中支持之前和之后的兩個副本)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/343932.html
標籤:混帐
上一篇:精準測驗系列分享之一:JaCoCo 企業級應用的優缺點分析
下一篇:必須掌握的微信小程式測驗點
