问题
I have deleted a file or some code in a file sometime in the past. Can I grep in the content (not in the commit messages)?
A very poor solution is to grep the log:
git log -p | grep <pattern>
However, this doesn\'t return the commit hash straight away. I played around with git grep
to no avail.
回答1:
To search for commit content (i.e., actual lines of source, as opposed to commit messages and the like), you need to do:
git grep <regexp> $(git rev-list --all)
git rev-list --all | xargs git grep <expression>
will work if you run into an "Argument list too long" error.
If you want to limit the search to some subtree (for instance, "lib/util"), you will need to pass that to the rev-list
subcommand and grep
as well:
git grep <regexp> $(git rev-list --all -- lib/util) -- lib/util
This will grep through all your commit text for regexp
.
The reason for passing the path in both commands is because rev-list
will return the revisions list where all the changes to lib/util
happened, but also you need to pass to grep
so that it will only search in lib/util
.
Just imagine the following scenario: grep
might find the same <regexp>
on other files which are contained in the same revision returned by rev-list
(even if there was no change to that file on that revision).
Here are some other useful ways of searching your source:
Search working tree for text matching regular expression regexp:
git grep <regexp>
Search working tree for lines of text matching regular expression regexp1 or regexp2:
git grep -e <regexp1> [--or] -e <regexp2>
Search working tree for lines of text matching regular expression regexp1 and regexp2, reporting file paths only:
git grep -e <regexp1> --and -e <regexp2>
Search working tree for files that have lines of text matching regular expression regexp1 and lines of text matching regular expression regexp2:
git grep -l --all-match -e <regexp1> -e <regexp2>
Search working tree for changed lines of text matching pattern:
git diff --unified=0 | grep <pattern>
Search all revisions for text matching regular expression regexp:
git grep <regexp> $(git rev-list --all)
Search all revisions between rev1 and rev2 for text matching regular expression regexp:
git grep <regexp> $(git rev-list <rev1>..<rev2>)
回答2:
You should use the pickaxe (-S) option of git log
To search for Foo
:
git log -SFoo -- path_containing_change
git log -SFoo --since=2009.1.1 --until=2010.1.1 -- path_containing_change
See Git history - find lost line by keyword for more.
As Jakub Narębski commented:
this looks for differences that introduce or remove an instance of
<string>
.
It usually means "revisions where you added or removed line with 'Foo'".the
--pickaxe-regex
option allows you to use extended POSIX regex instead of searching for a string.
As Rob commented, this search is case-sensitive - he opened a follow-up question on how to search case-insensitive.
回答3:
My favorite way to do it is with git log
's -G
option (added in version 1.7.4).
-G<regex>
Look for differences whose added or removed line matches the given <regex>.
There is a subtle difference between the way the -G
and -S
options determine if a commit matches:
- The
-S
option essentially counts the number of times your search matches in a file before and after a commit. The commit is shown in the log if the before and after counts are different. This will not, for example, show commits where a line matching your search was moved. - With the
-G
option, the commit is shown in the log if your search matches any line that was added, removed, or changed.
Take this commit as an example:
diff --git a/test b/test
index dddc242..60a8ba6 100644
--- a/test
+++ b/test
@@ -1 +1 @@
-hello hello
+hello goodbye hello
Because the number of times "hello" appears in the file is the same before and after this commit, it will not match using -Shello
. However, since there was a change to a line matching hello
, the commit will be shown using -Ghello
.
回答4:
If you want to browse code changes (see what actually has been changed with the given word in the whole history) go for patch
mode - I found a very useful combination of doing:
git log -p
# hit '/' for search mode
# type in the word you are searching
# if the first search is not relevant hit 'n' for next (like in vim ;) )
回答5:
I took @Jeet's answer and adpated it to Windows (thanks to this answer):
FOR /F %x IN ('"git rev-list --all"') DO @git grep <regex> %x > out.txt
Note that for me, for some reason, the actual commit that deleted this regex did not appear in the output of the command, but rather one commit prior to it.
回答6:
git log
can be a more effective way of searching for text across all branches, especially if there are many matches, and you want to see more recent (relevant) changes first.
git log -p --all -S 'search string'
git log -p --all -G 'match regular expression'
These log commands list commits that add or remove the given search string/regex, (generally) more recent first. The -p
option causes the relevant diff to be shown where the pattern was added or removed, so you can see it in context.
Having found a relevant commit that adds the text you were looking for (eg. 8beeff00d), find the branches that contain the commit:
git branch -a --contains 8beeff00d
回答7:
Search in any revision, any files:
git rev-list --all | xargs git grep <regexp>
Search only in some given files, for example XML files:
git rev-list --all | xargs -I{} git grep <regexp> {} -- "*.xml"
The result lines should look like this: 6988bec26b1503d45eb0b2e8a4364afb87dde7af:bla.xml: text of the line it found...
You can then get more information like author, date, diff using git show
:
git show 6988bec26b1503d45eb0b2e8a4364afb87dde7af
回答8:
For anyone else trying to do this in Sourcetree, there is no direct command in the UI for it (as of version 1.6.21.0). However, you can use the commands specified in the accepted answer by opening Terminal window (button available in the main toolbar) and copy/pasting them therein.
Note: Sourcetree's Search view can partially do text searching for you. Press Ctrl + 3 to go to Search view (or click Search tab available at the bottom). From far right, set Search type to File Changes and then type the string you want to search. This method has the following limitations compared to the above command:
- Sourcetree only shows the commits that contain the search word in one of the changed files. Finding the exact file that contains the search text is again a manual task.
- RegEx is not supported.
回答9:
For simplicity, I'd suggest using GUI: gitk - The Git repository browser. It's pretty flexible
- To search code:
- To search files:
- Of course, it also supports regular expressions:
And you can navigate through the results using the up/down arrows.
回答10:
@Jeet's answer works in PowerShell.
git grep -n <regex> $(git rev-list --all)
The following displays all files, in any commit, that contain a password
.
# store intermediate result
$result = git grep -n "password" $(git rev-list --all)
# display unique file names
$result | select -unique { $_ -replace "(^.*?:)|(:.*)", "" }
回答11:
So are you trying to grep through older versions of the code looking to see where something last exists?
If I were doing this, I would probably use git bisect. Using bisect, you can specify a known good version, a known bad version, and a simple script that does a check to see if the version is good or bad (in this case a grep to see if the code you are looking for is present). Running this will find when the code was removed.
回答12:
git rev-list --all | xargs -n 5 git grep EXPRESSION
is a tweak to @Jeet's solution, so it shows results while it searches and not just at the end (which can take a long time in a large repository).
回答13:
Scenario: You did a big clean up of your code by using your IDE. Problem: The IDE cleaned up more than it should and now you code does not compile (missing resources, etc.)
Solution:
git grep --cached "text_to_find"
It will find the file where "text_to_find" was changed.
You can now undo this change and compile your code.
回答14:
Whenever I find myself at your place, I use the following command line:
git log -S "<words/phrases i am trying to find>" --all --oneline --graph
Explanation:
git log
- Need I write more here; it shows the logs in chronological order.-S "<words/phrases i am trying to find>"
- It shows all those Git commits where any file (added/modified/deleted) has the words/phrases I am trying to find without '<>' symbols.--all
- To enforce and search across all the branches.--oneline
- It compresses the Git log in one line.--graph
- It creates the graph of chronologically ordered commits.
回答15:
In my case I needed to search a Short Commit and the listed solutions were unfortunately not working.
I managed to do it with: (replace the REGEX token)
for commit in $(git rev-list --all --abbrev-commit)
do
if [[ $commit =~ __REGEX__ ]]; then
git --no-pager show -s --format='%h %an - %s' $commit
fi
done
来源:https://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history