Ben G
Ben G

Reputation: 26789

Searching for a file in all Git branches

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.

Upvotes: 33

Views: 20465

Answers (2)

Eric O. Lebigot
Eric O. Lebigot

Reputation: 94595

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

Upvotes: 10

manojlds
manojlds

Reputation: 301527

This is one way:

git log --all --name-only --pretty=format: | sort -u | grep _robot.php

Upvotes: 37

Related Questions