ESC

Type to search the knowledge base.

Cheat sheetGit

Git

Daily commands, branching, undo patterns, and interview-safe explanations.

Everyday

bash
git status
git diff
git diff --staged
git add -p                 # stage hunks
git commit -m "message"
git log --oneline --graph -20
git switch branch-name
git switch -c feature/x

Sync

bash
git fetch origin
git pull                   # fetch + merge (or rebase if configured)
git pull --rebase
git push -u origin HEAD
git push --force-with-lease  # safer than --force

Undo (choose carefully)

git restore fileDiscard unstaged work in file
git restore --stagedUnstage, keep changes
git reset --soft HEAD~1Undo commit, keep staged
git reset HEAD~1Undo commit, keep unstaged
git revert <sha>New commit that undoes a commit (safe for shared)

Rebase & stash

bash
git fetch origin
git rebase origin/main
git rebase -i HEAD~3       # squash/edit locally
git stash -u
git stash pop

Interview tips

  • Explain merge vs rebase: history shape vs linear story; never rewrite shared main.
  • Prefer --force-with-lease over --force.
  • revert for public history; reset for local only.

Common mistakes

  • !Force-pushing rewritten main/master.
  • !Huge unrelated commits — reviewers hate it.
  • !Committing secrets; rotate keys immediately if you do.

Related

← All cheat sheets