Get in touch

Send an email to: lammers@gmail.com.
Or find me online at: Github, Twitter/X

Search in history with git log

Besides listing out all the commits, git log can also be used to search for code in commits. Which is very helpful when trying to find out when a certain piece of code has been introduced in the codebase. This can be done by calling git log with the -S flag and a search term (string). The result will be all the commits containing the provided search term.
By default only commits in the current branch will be shown. To search in all branches use the --all flag.

# Search for all the commits containing `myFunc`
$ git log -S 'myFunc'

# Search only inside the src/ folder
$ git log -S 'myFunc' src/

# Seach in all branches
$ git log -S 'myFunc' --all

Instead of just showing the commits, it's often useful to also show the code that changed in the found commits. This can be done by adding the -p flag.

# Show the commits containing `myFunc` including the changed code
$ git log -S 'myFunc' -p