Skip to content

Instantly share code, notes, and snippets.

@thomaschaplin
Created August 21, 2025 11:40
Show Gist options
  • Select an option

  • Save thomaschaplin/eaeb0f6b4b74e73c22cc1d1ce6743077 to your computer and use it in GitHub Desktop.

Select an option

Save thomaschaplin/eaeb0f6b4b74e73c22cc1d1ce6743077 to your computer and use it in GitHub Desktop.
Git Commit Search Cheat Sheet

Git Commit Search Cheat Sheet

Quick reference for finding commits in Git with examples.


1. Search by exact string (-S)

git log -p -S "<string>" -- <file>
  • Shows commits where <string> was added or removed.
  • Example:
git log -p -S "arg.selectionSet.selections" -- src/index.ts

Example diff (color-coded)

commit abc123
Author: Dev <dev@example.com>
Date:   2025-08-21

+  console.log(arg.selectionSet.selections)   # Added line (green)
-  console.log(arg.selectionSet.foo)          # Removed line (red)

Only commits adding or removing the exact string appear.


2. Search by pattern/regex (-G)

git log -p -G "<regex>" -- <file>
  • Shows commits where lines matching regex were changed.
  • Example:
git log -p -G "selectionSet" -- src/index.ts

Example diff (color-coded)

commit def456
Author: Dev <dev@example.com>
Date:   2025-08-20

-  console.log(arg.selectionSet)              # Removed (red)
+  console.log(arg.selectionSet.selections)   # Added (green)

Any change to lines containing "selectionSet" is included.


3. Search all branches

git log -p --all -S "arg.selectionSet.selections" -- src/index.ts
git log -p --all -G "selectionSet" -- src/index.ts

4. Optional filters

--author="<name>"      # Filter by author
--since="YYYY-MM-DD"   # Filter by date
--oneline              # One-line summary per commit

5. Quick Comparison

Option Finds Use case
-S Commits that add/remove exact string Track when a variable or function was introduced or removed
-G Commits that modify lines matching regex Track all changes related to a pattern, even partial changes

Tip

Combine with --oneline for faster browsing:

git log -S "myVar" --oneline

Visual Reminder

  • Green (+) = lines added
  • Red (-) = lines removed
  • -S → exact string change only
  • -G → any line matching pattern changed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment