I'm looking for a tool, or for suggestions towards a script, that would be able to search a Git repository for files based on both filenames and file contents (find/grep-like). It would need to be able to search not just in the currently checked out branch, but through that branch's history as well as in other branches.

The very specific example of a Git repository that I have is a checkout of dspinellis' unix-history-repo. I often look for historical implementations of things in there, but it's a pain to track down files as I often need to guess what branch I need to be looking at (there are 165 branches in that repository).

Example of thing I would like to do with this tool is to find the wow command, which may have been an external command or a built-in command in sh or csh at some point, if it existed as part of some early BSD Unix (if it existed at all). To do this, I would want to search for wow.* as a filename pattern, and also for files that may at some point have included a wow C function, across the 165 branches of dspinellis' Git repository.


Heh, guess what I’ve been doing too...

To look for a file name across all branches, I use

git log --all --name-only --pretty=format:%H -- wow\*

wow can be replaced by any glob. This runs quite quickly on the Unix history repository. The format shows the hash leading to the creation of the matching file, so you can then check the tree out at that point and explore further.

To search file contents across all branches, I use

git rev-list --all | xargs git grep "excited too"

which lists all commit objects and searches through them. This is very, very slow on the Unix history repository; listing all the branches and grepping there is quicker:

git grep "excited too" $(git branch -r | awk '{print $1}')
  • This is very useful! Thanks Stephen. I will see if I can wrap these commands in convenience functions... The git rev-list --all command outputs exactly 400200 hashes (for this particular repository), so yes, I can imagine that it would be rather slow. It would include the older commits of each branch though (I presume), i.e. not just the latest version, so the search would be more thorough. – Kusalananda Jan 10 at 16:22
  • Yes, it would be more thorough ;-). Another approach specifically with the Unix history repo would be to use the source repo and associated downloads, and search those instead. – Stephen Kitt Jan 10 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.