Git Cheatsheet
Viewing History
Use this Git reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
git log Basics
git log # Full log: commit, author, date, message git log --oneline # Condensed: short SHA + subject line git log --oneline --graph # ASCII graph of branches and merges git log --oneline --graph --all # Include all branches git log --oneline --decorate # Show branch/tag labels next to commits git log --oneline --graph --decorate --all # All of the above (common alias) git log -n 5 # Show only the last 5 commits git log -5 # Shorthand git log --reverse # Show oldest commits first
Formatting Output
git log --format="%H %s" # Custom format: full SHA + subject git log --pretty=oneline # Built-in formats: oneline, short, medium, full, fuller, raw, email git log --pretty=short git log --pretty=full git log --pretty=fuller git log --pretty=format:"%h - %an, %ar : %s" # Example custom format
Common --format / --pretty=format: Placeholders
| Placeholder | Meaning |
|---|---|
%H | Full commit SHA |
%h | Abbreviated SHA |
%T / %t | Tree SHA (full / abbreviated) |
%P / %p | Parent SHAs (full / abbreviated) |
%an | Author name |
%ae | Author email |
%ad | Author date (format with --date=) |
%ar | Author date, relative |
%cn | Committer name |
%cd | Committer date |
%s | Subject (first line of message) |
%b | Body of commit message |
%d | Ref names (like --decorate) |
%Cred / %Cgreen / %Creset | ANSI color codes |
# Pretty one-liner with color git log --pretty=format:"%C(yellow)%h%Creset %s %C(blue)(%an)%Creset" --all
Filtering Commits
git log --author="Alice" # By author name (partial match, regex OK) git log --author="Alice\|Bob" # Multiple authors git log --committer="Alice" # By committer git log --grep="fix" # Subject/body contains "fix" (case-sensitive) git log --grep="fix" -i # Case-insensitive grep git log --all-match --grep="bug" --grep="login" # Must match all patterns # By date git log --since="2024-01-01" git log --until="2024-06-30" git log --after="1 week ago" git log --before="yesterday" git log --since="2024-01-01" --until="2024-03-01" # By file git log -- <file> # Commits that touched a specific file git log -- <dir>/ # Commits touching any file in a directory git log --follow -- <file> # Follow renames across history # By content change (pickaxe) git log -S "functionName" # Commits that added/removed this string git log -G "regex.*pattern" # Commits whose diff matches a regex # By range git log <branch1>..<branch2> # Commits in branch2 not in branch1 git log <sha1>..<sha2> # Between two commits git log ^main feature # Commits in feature reachable but not from main git log origin/main..HEAD # Commits not yet pushed to origin/main
Showing Diffs in Log
git log -p # Show full patch for each commit git log -p <file> # Patches only for a specific file git log --stat # Show files changed + insertions/deletions summary git log --shortstat # More concise stat (one line per commit) git log --name-only # Just the names of changed files git log --name-status # Changed files with M/A/D status codes git log --diff-filter=A # Only commits that Added files git log --diff-filter=D # Only commits that Deleted files git log --diff-filter=M # Only commits that Modified files git log --diff-filter=R # Only commits with Renames git log --diff-filter=C # Only commits with Copies
git show
git show # Show latest commit + diff git show <sha> # Show a specific commit git show <sha> --stat # Summary of changed files git show <sha>:<file> # Show file content as of that commit git show HEAD~3 # Show commit 3 back from HEAD git show main~2 # Show 2 commits before main tip git show HEAD^ # Show parent of HEAD git show HEAD^^ # Show grandparent of HEAD git show HEAD^2 # Show second parent (for merges) git show v1.2.0 # Show tag
Diffing
git diff # Unstaged changes git diff --staged # Staged changes vs. last commit git diff HEAD # All changes (staged + unstaged) vs. last commit git diff <sha> <sha> # Between two commits git diff main..feature # Between two branches git diff main...feature # From common ancestor to feature tip (three-dot) git diff HEAD~3..HEAD # Last 3 commits git diff -- <file> # Diff a specific file only git diff --stat # Summary instead of full diff git diff --word-diff # Word-level diff (easier for prose) git diff --color-words # Highlight changed words inline git diff -U5 # Show 5 lines of context (default 3) git diff --ignore-space-change # Ignore amount of whitespace change git diff --ignore-all-space # Ignore all whitespace git diff --name-only # Just the filenames git diff --name-status # Filenames with M/A/D codes
Searching History
git log -S "string" # Find when a string was added/removed (pickaxe) git log -G "regex" # Find when diff matched a regex git log --all --grep="keyword" # Search commit messages across all branches git grep "pattern" # Search working tree for a pattern git grep "pattern" <sha> # Search a specific commit's tree git grep -n "pattern" # Show line numbers git grep -c "pattern" # Show count per file git grep -l "pattern" # List filenames only
git blame
git blame <file> # Show who last changed each line git blame -L 10,20 <file> # Blame only lines 10–20 git blame -L 10,+5 <file> # Blame 5 lines starting at line 10 git blame -w <file> # Ignore whitespace changes git blame -M <file> # Detect moved lines within the file git blame -C <file> # Detect lines copied from other files git blame --since="1 year ago" <file> # Blame as of a date git blame <sha> -- <file> # Blame at a specific commit git blame -e <file> # Show email instead of author name
git bisect (Binary Search for a Bug)
git bisect start # Start a bisect session git bisect bad # Mark current commit as bad git bisect good <sha> # Mark a known-good commit # Git checks out the midpoint; test it, then: git bisect good # Mark midpoint as good git bisect bad # Mark midpoint as bad # Repeat until Git identifies the first bad commit git bisect reset # End bisect session, return to original HEAD # Automated bisect with a test script git bisect run <script> # Script returns 0=good, 1=bad, 125=skip git bisect run npm test # Example: use test suite git bisect skip # Skip a commit (e.g., broken build) git bisect log # Show bisect session history git bisect replay <file> # Replay a bisect log
Viewing the Reflog
git reflog # All recent HEAD positions git reflog <branch> # Reflog for a specific branch git reflog --relative-date # Human-readable timestamps git reflog show --all # All refs
Shorthand Revision Syntax
| Syntax | Meaning |
|---|---|
HEAD | Current commit |
HEAD~1 or HEAD~ | One commit before HEAD |
HEAD~3 | Three commits before HEAD |
HEAD^ | First parent of HEAD |
HEAD^2 | Second parent of HEAD (merge) |
main@{1} | Previous position of main (reflog) |
main@{yesterday} | main as of yesterday (reflog) |
@{-1} | Previously checked out branch |
v1.0 | Tag |
:/fix typo | Most recent commit whose message matches |