Git
Look before you leap: git status and git log --oneline -5 before any destructive action.
Inspect
git status
git diff # unstaged changes
git diff --staged # staged changes
git log --oneline -10
git log --oneline --graph --all -20
Branch & commit
git switch -c feature/x # create + switch (modern; = checkout -b)
git add -p # stage hunks interactively
git commit -m "message"
git switch main && git merge feature/x
Undo safely (most common rescues)
git restore <file> # discard unstaged changes to a file
git restore --staged <file> # unstage (keep changes)
git commit --amend # fix the last commit message/contents (only if unpushed)
git reset --soft HEAD~1 # undo last commit, KEEP changes staged
git revert <sha> # safe undo of a pushed commit (new commit)
git reflog # find "lost" commits after a bad reset
Merge conflicts
git status # see conflicted files
# edit files, remove <<<<<<< ======= >>>>>>> markers, keep the right code
git add <file> && git commit # (or: git rebase --continue)
git merge --abort # bail out and start over
Guidance
- Prefer
git revertovergit reset --hardfor anything already pushed/shared. reset --hardandclean -fdare destructive — confirm the branch and stash first (git stash).- Write small, focused commits with imperative messages ("Add X", not "added X").
- When unsure where you are:
git status,git branch,git reflogorient you.