Git Cheatsheet: From Basics to Time Travel
Git Cheatsheet: From Basics to Time Travel
A collection of essential Git commands, from daily workflows to digging through the depths of your repository's history.
🔍 Searching History
Find when a file existed (even if deleted)
bashgit log --all -- [path]
Search for content changes (Pickaxe)
Find commits where a specific string was added or removed:
bashgit log -S "your_search_string"
Search content with Regex
bashgit log -G "your_regex"
Find a file in any commit/branch
bashgit rev-list --all | xargs git grep -l "filename"
See the history of a specific function/method
bashgit log -L :function_name:file_path
🚀 Daily Workflow
Stage and Commit
bashgit add . git commit -m "feat: descriptive message"
Undo last commit (keep changes)
bashgit reset --soft HEAD~1
Fix the last commit message
bashgit commit --amend -m "new message"
🌿 Branching & Merging
Switch to a new branch
bashgit checkout -b feature/cool-stuff # or the newer way: git switch -c feature/cool-stuff
List all branches (including remote)
bashgit branch -a
Safely delete a branch
bashgit branch -d branch_name
🛠️ Cleanup & Maintenance
Discard all local changes
bashgit reset --hard HEAD
Clean untracked files
bashgit clean -fd
Stash changes for later
bashgit stash save "Work in progress" git stash list git stash pop
📤 Remote Operations
Update local with remote and rebase
bashgit pull --rebase origin main
Prune old remote tracking branches
bashgit fetch -p