Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for a file in all Git branches

Tags:

git

grep

How do I search all Git branches of a project for a file name?

I remember part of the filename (just the ending), so I'd like to be able to search for something like *_robot.php across all branches, and see which files match that. I'd preferably like to have it search history, and not just the HEADs of branches.

like image 486
bgcode Avatar asked Dec 05 '11 20:12

bgcode


People also ask

How do I search for a specific file in git?

As stated, use gitk --all, then in View | New view, enable All Branches. Then set your search criteria: filenames (with wild cards) in the penultimate field. Finally: OK.

What is git grep?

`git grep` command is used to search in the checkout branch and local files. But if the user is searching the content in one branch, but the content is stored in another branch of the repository, then he/she will not get the searching output.

What is the git command to see all the remote branches?

To view your remote branches, simply pass the -r flag to the git branch command. You can inspect remote branches with the usual git checkout and git log commands.


2 Answers

This is one way:

git log --all --name-only --pretty=format: | sort -u | grep _robot.php 
like image 125
manojlds Avatar answered Oct 05 '22 01:10

manojlds


Here is a simpler variation on @manojlds's solution: Git can indeed directly consider all the branches (--all), print the names of their modified files (--name-only), and only these names (--pretty=format:).

But Git can also first filter certain file names (regular expression) by putting the file name regular expression after the -- separator:

git log --all --name-only --pretty=format: -- <file_name_regexp> | sort -u 

So, you can directly do:

git log --all --name-only --pretty=format: -- _robot.php | sort -u 
like image 22
Eric O Lebigot Avatar answered Oct 05 '22 00:10

Eric O Lebigot