Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search git Commits Using Regex

I have a git repository that contains hundreds of commits and several branches. How to search a particular commit that contains a certain string e.g. "helper function"? Ideally, the string can be denoted by a regex.

like image 246
moey Avatar asked Jan 28 '12 13:01

moey


People also ask

Which flag is used to filter commits by pattern or regular expression?

To filter commits by their commit message, use the --grep flag.

How do I search for text in git?

git log -p # Hit '/' for search mode. # Type in the word you are searching.

What is grep in git?

Git Grep. Git ships with a command called grep that allows you to easily search through any committed tree, the working directory, or even the index for a string or regular expression. For the examples that follow, we'll search through the source code for Git itself.

How do I search for a specific commit in Git?

To search the commit log (across all branches) for the given text: git log --all --grep='Build 0051' To search the actual content of commits through a repo's history, use: git grep 'Build 0051' $ (git rev-list --all)

How to search specific content in Git repository using regular expression?

Any git repository contains many files, folders, branches, tags, etc. Sometimes it requires searching the particular content in the git repository using a regular expression pattern. `git grep` command is used to search in the checkout branch and local files.

How do I use regular expressions in git log?

If Git can’t figure out how to match a function or method in your programming language, you can also provide it with a regular expression (or regex ). For example, this would have done the same thing as the example above: git log -L '/unsigned long git_deflate_bound/',/^}/:zlib.c .

How can I limit the number of commits in git log?

git log --grep=<pattern> Limit the commits output to ones with log message that matches the specified pattern (regular expression). Show activity on this post. Try this!


1 Answers

Newer versions of git support git log -G<regex>:

git log -G'helper.*function' --full-history --all 

it will search for the regex in the diff of each commit, and only display commits which introduced a change that matches the regex.

like image 66
knittl Avatar answered Sep 22 '22 15:09

knittl