As repositories grow, finding specific changes, understanding when a line of code was introduced, and tracking commit history becomes essential. Git provides robust tools for searching file contents (git grep) and auditing commit history (git log and git blame).
Searches all tracked files in your working directory for a specified keyword or phrase (much faster than traditional file search).
git grep "[search-query]"# Search for standard API endpoints
git grep "fetchUserData"Searches file contents within a historical commit, tag, or branch instead of your active working directory.
git grep "[search-query]" <commit-hash-or-tag-or-branch>git grep "StripeCheckout" v2.5.0Audits commit history to find exactly when a keyword or variable name was added or deleted from any file (referred to as Git Pickaxe).
git log -S "[keyword]"git log -S "secret_api_key"Uses Git Pickaxe with regular expressions to identify commits introducing specific code patterns.
git log -S "[regex]" --pickaxe-regexDisplays the complete commit history, starting with the newest, showing full hashes, author details, dates, and messages.
git logShows a compressed view of commit history (abbreviated hash and commit message) for quick scannability.
git log --onelineFilters the commit history to display only commits authored by a specific developer.
git log --author="[author-name]"git log --author="Tanmay"Displays every commit that modified a specific file, along with full patch details (exact line diffs).
git log -p <filename>git log -p src/utils/auth.jsShows commits that are present in one branch/remote but missing in another (using dot ranges).
git log --oneline <branch1>..<branch2>git log --oneline main..origin/mainAnnotates each line of a file with the commit ID, author name, and date of the change. Excellent for finding out who wrote a line of code and why.
git blame <filename>git blame package.jsonShows the log of all movements of the local repository HEAD pointer (resets, commits, checkouts, merges). Reflog is a local safety net: it tracks everything you do, even if you delete a branch or hard reset a commit.
git reflogProblem: You ran git log and your terminal froze with a colon (:) at the bottom, and typing commands doesn't work.
Solution: Git uses a terminal pager (usually less) for long outputs. Simply press q to quit and return to your prompt.
Problem: Running git log <file> shows an empty log or very short history because the file was renamed in the past.
Solution: Append the --follow flag to instruct Git to search history past the rename boundaries:
git log --follow <file>- Use graphical visualizations: Make terminal logs easy to read by adding formatting options:
git log --oneline --graph --all --decorate
- Use reflog as a safety net: Remember that Git rarely deletes anything permanently. If you run a destructive
git reset --hardand lose commits, rungit reflogimmediately to find the old commit hash and restore it.