Need to find that one commit that fixed a bug or introduced chaos and you do not want to read the entire history by candlelight? Use git log and grep like a civilized developer. This guide shows practical commands and small bits of sarcasm to keep you awake while you search commit messages in version control from the CLI.
The simplest trick is to let git filter commit messages for you. The --grep flag matches the commit message text with a regular expression. It is fast and often all you need.
git log --grep='fix crash'
That returns commits whose messages match the pattern. No witchcraft, just grep style matching built into git log.
People type commit messages in ALL CAPS, Title Case, and weird emoji hieroglyphs. Use -i to make your search case insensitive.
git log -i --grep='memory leak'
Now Fix Memory Leak and fix memory leak are equals. Peace at last.
If the commit is hiding on another branch or a tag please do not assume it will come to you. Use --all to search everywhere.
git log --all --grep='refactor'
This covers branches and tags so there is no accidental tunnel vision.
When history gets chatty use a compact view so you can scan results quickly.
git log --pretty=oneline --grep='typo'
One line per commit shows the hash and message so you can eyeball the candidate commits without squinting.
If the history spits out a novella add -n to cap the results to the most recent matches.
git log -n 5 --grep='add tests'
This shows the latest five matches and spares you from scrolling forever.
Mix and match the options to suit your workflow. Here is a typical combination that is both practical and slightly smug.
git log --all -i --pretty=oneline -n 10 --grep='bug fix'
That searches every ref with case insensitive matching, shows concise results, and limits output to the ten most recent hits. Efficient and tidy.
If you want to find commits that changed the code rather than the commit message try git log -S. It looks for changes in the patch text rather than the message. That matters when the message is useless and the code tells the truth.
git log -S 'calculateTotal' --pretty=oneline
Sometimes knowing who wrote the commit narrows things down. Use --author to filter by committer or author name.
git log --author='Alex' --grep='perf'
If you remember one thing let it be this. Search with --grep and tune with -i --all --pretty and -n until the result set looks human sized. That will save you time and dignity. Now go find the commit and pretend you always knew where it was.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.