Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git grep/search a specific branch

Tags:

git

I am looking for a way to grep a certain branch. Simple?

To show a specific file in a branch, one would use:

git show branch:my_branch bla_sheep.txt

Similar syntax is used to diff, etc.

Is there a way to simply grep a branch:

git grep branch:my_branch <regex>
like image 266
p0lAris Avatar asked Nov 11 '15 23:11

p0lAris


People also ask

Does git grep search all branches?

Git includes a grep command to search through commits to a repo as well as the local files in the repo directory: git grep. Sometimes it is useful to search for a string throughout an entire repo, e.g. to find where an error message is produced.

Can you grep github?

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.


2 Answers

To look at multiple commits, starting at some branch tip and working backwards: git grep can look in any particular tree (hence any particular commit since any given commit identifies a particular tree), but to iterate over every commit contained within some branch, you will need to do your own git rev-list:

git rev-list branch | while read rev; do git grep <regexp> $rev; done

for instance (but you may want to fancy this up more so that your grep stops once you've found what you are looking for).

If you want to look once, at the commit that is the tip of branch <branch>, and find (or fail to find) any instances of <regexp>, just do a simple:

git grep <regexp> branch

The loop form is only for the case where you want to start at that tip commit and keep working backwards through previous commits on the branch as well.

like image 150
torek Avatar answered Oct 23 '22 02:10

torek


To grep another branch use:

git grep <regex> <branch-name>
like image 39
sergej Avatar answered Oct 23 '22 03:10

sergej