Git Cheat Sheet¶
Fun fact: Git was created by Linus Torvalds in 2005 in just two weeks, after the Linux kernel project lost access to the proprietary BitKeeper VCS. The name "git" is British slang for an unpleasant person — Torvalds said "I name all my projects after myself."
Daily Workflow¶
git status # What's changed?
git add -p # Stage interactively (hunk by hunk)
git commit -m "message" # Commit
git push origin branch # Push
git pull --rebase origin main # Update from main (rebase)
# Gotcha: --rebase replays your commits on top of remote, keeping history linear.
# Without it, pull creates merge commits that clutter the log.
Remember:
git add -p(patch mode) is the most underused git feature. It lets you stage individual hunks within a file — so you can commit related changes separately even if they are in the same file. This is the key to clean, focused commits.
Branching¶
git branch feature # Create branch
git checkout -b feature # Create + switch
git switch -c feature # Modern equivalent
git branch -d feature # Delete (safe)
git branch -D feature # Delete (force)
git branch -a # List all (including remote)
git merge feature # Merge into current
git rebase main # Rebase onto main
Undoing Things¶
# Unstage a file
git restore --staged file.txt
# Discard working dir changes
git restore file.txt
# Undo last commit (keep changes staged)
git reset --soft HEAD~1
# Undo last commit (unstage changes)
git reset HEAD~1
# Undo last commit (discard changes) — DESTRUCTIVE, no recovery without reflog
git reset --hard HEAD~1
# Create a reverse commit (safe for shared branches)
git revert HEAD
git revert abc1234
Interactive Rebase¶
git rebase -i HEAD~5
git rebase -i main
# Commands:
# pick — keep commit
# squash — merge into previous (keep message)
# fixup — merge into previous (discard message)
# reword — change commit message
# drop — delete commit
# edit — pause for amending
Stash¶
git stash # Save working changes
git stash pop # Restore + remove from stash
git stash apply # Restore + keep in stash
git stash list # List stashes
git stash show -p stash@{0} # Show diff of stash
git stash drop stash@{0} # Delete specific stash
git stash push -m "message" # Named stash
History & Investigation¶
git log --oneline --graph -20 # Visual history
git log --author="name" # By author
git log -p -- file.txt # Changes to a file
git log -S "function_name" # When string was added/removed
git blame file.txt # Line-by-line authorship
git bisect start # Binary search for bug
git bisect bad # Current is broken
git bisect good v1.0 # v1.0 was working
Cherry-pick¶
git cherry-pick abc1234 # Apply one commit
git cherry-pick abc..def # Apply range
git cherry-pick -n abc1234 # Apply without committing
Reflog (Safety Net)¶
git reflog # Show all HEAD movements
git reflog show feature # For a specific branch
# Recover from mistakes
git reset --hard HEAD@{2} # Go back 2 moves
git checkout -b recover HEAD@{5} # Create branch from old state
Remote Operations¶
git remote -v # List remotes
git remote add upstream URL # Add remote
git fetch origin # Fetch without merge
git push -u origin feature # Push + set upstream
# Force push (dangerous!)
git push --force-with-lease # Safer: fails if remote changed
Merge vs Rebase¶
Merge: preserves history, creates merge commit
git checkout main && git merge feature
Rebase: linear history, rewrites commits
git checkout feature && git rebase main
Rule of thumb:
- Rebase for local/feature branches
- Merge for shared/main branches
- Never rebase commits that others have pulled
Config¶
git config --global user.name "Name"
git config --global user.email "email"
git config --global core.editor vim
git config --global pull.rebase true # Default to rebase on pull
git config --list --show-origin # See all settings + source
.gitignore Patterns¶
*.log # All .log files
!important.log # Except this one
build/ # Directory
**/temp/ # temp/ at any depth
*.py[cod] # .pyc, .pyo, .pyd
.env # Secrets file