keyboard-shortcut
d

git search 🔎

2min read

an image

Looking for something?

git blame is amazing at helping to explain something that exists. Sometimes though, you'll be trying to identify why something doesn't exist. How do you git information on something that doesn't exist?

An example

You can search all git commits that include a particular string. Recently I found a private method (i.e. not intended to be used outside of the class) but no reference was made to this method within the class. Was there a time when this method was used?

Enter git pickaxe!! 🪓

  • git log -S 'string' - shows commits where a line containing 'string' was added or deleted
  • git log -G 'string' - shows commits where a line containing 'string' was added, deleted or moved.

Searching with regex 🔍

But Alex, the information I'm looking for isn't as clear cut. Can I do some sort of fuzzy search? Yes! You can use regexes if you need a bit of extra flexibility in your search.

  • git log -S 'regex' --pickaxe-regex - shows commits where a line matching 'regex' was added or deleted.
  • git log -G 'regex' - shows commits where a line matching 'regex' was added, deleted or moved (-G defaults to a regex).

Searching commit messages

But Alex, the thing I'm looking for is more conceptual... is there a nice way to search only the commit messages so I can find a conceptual change?

  • git log --grep 'reg\Sx' - shows commits where the message matches the regex.

Searching for lost files

But Alex, I don't know what the contents of the file were, just the name of the file.

  • git log -- file_that_no_longer_exists.txt

Hhmmm.... actually, I don't know the name of the file... Can you just show which files were deleted in each commit please?

  • git log --diff-filter=D --summary --pretty=format:"%H %s"

What else?

If you still can't find what you're looking for... well... ideas are welcome!