TL;DR
- Git tracks snapshots, not diffs. Commits are immutable.
- Stage → Commit → Push workflow. Branch early, branch often.
git log --oneline --graph is your best friend.
Daily Workflow
git status # What's changed?
git add . # Stage everything
git add -p # Stage interactively (hunk by hunk)
git commit -m "feat: add X" # Commit with message
git push # Push to remote
Branching
git branch feature/auth # Create branch
git switch feature/auth # Switch to it
git switch -c feature/auth # Create + switch (shortcut)
git branch -d feature/auth # Delete (safe)
git branch -D feature/auth # Force delete
Undo Things
| Want to... |
Command |
| Unstage a file |
git restore --staged file.txt |
| Discard local changes |
git restore file.txt |
| Undo last commit (keep changes) |
git reset --soft HEAD~1 |
| Undo last commit (discard) |
git reset --hard HEAD~1 |
| Revert a pushed commit |
git revert <sha> |
| Amend last commit message |
git commit --amend -m "new msg" |
Merge vs Rebase
# Merge — creates merge commit, preserves history
git switch main
git merge feature/auth
# Rebase — linear history, rewrites commits
git switch feature/auth
git rebase main
# then fast-forward merge
git switch main
git merge feature/auth
Stash
git stash # Save dirty working dir
git stash pop # Apply + remove from stash
git stash list # See all stashes
git stash drop # Remove latest stash
Inspect
git log --oneline --graph --all # Visual history
git diff # Unstaged changes
git diff --staged # Staged changes
git show <sha> # Show specific commit
git blame file.txt # Who wrote each line