$ webtoolist --read
Git Commands
Copy-friendly Git commands for the situations that actually come up: fixing commits, undoing mistakes, branches, and history archaeology.
Everyday workflow
git status # what changed, what's staged
git switch -c feat/login # create and switch to a new branch
git add -p # stage hunks interactively
git commit -m "Add login form"
git push -u origin feat/login
Amending and rewriting
git commit --amend # fold staged changes into the last commit
git commit --amend --no-edit # keep the same message
git rebase -i HEAD~5 # reword/squash/drop the last 5 commits
git rebase -i --autosquash main # auto-order fixup! commits made with --fixup
git pull --rebase origin main # replay your work on top of upstream
Undoing things
git restore file.txt # discard uncommitted changes to a file
git restore --staged file.txt # unstage, keep the changes
git reset --soft HEAD~1 # undo last commit, changes stay staged
git revert <sha> # safely undo a pushed commit with a new commit
git reset --hard origin/main # nuke local state to match remote
git reflog # find "lost" commits after a bad reset
git reset --hard HEAD@{2} # jump back to a reflog entry
Branches
git branch -d feat/login # delete a merged branch
git branch -D feat/login # force-delete an unmerged branch
git push origin --delete feat/login # delete the remote branch
git branch --merged main # branches safe to clean up
git switch - # back to the previous branch
git cherry-pick <sha> # copy one commit onto the current branch
History and archaeology
git log --oneline --graph --all # compact branch diagram
git log -p -- file.txt # full diffs for one file
git log -S "featureFlag" # find commits adding/removing that string
git blame -L 40,60 file.txt # who wrote these lines
git diff main...feature # changes on feature since it branched
git bisect start # binary-search for the commit that broke it
Stashing and cleanup
git stash push -m "wip: auth" # park changes with a label
git stash pop # restore the most recent stash
git stash apply stash@{2} # restore a specific stash, keep it
git worktree add ../hotfix main # second checkout for urgent fixes
Recovery one-shots
git commit --amend --author="Name <a@b.c>" # fix author of last commit
git rm -r --cached node_modules # untrack after .gitignore was added
git fetch --prune # drop remote-tracking refs for deleted branches
git gc --prune=now # compact the object store
Rules of thumb
- Anything already pushed to a shared branch: fix forward with
revert, neverpush --force. --force-with-leaseis the safer force push — it fails if someone pushed in the meantime.- When you think you lost work, check
git reflogbefore panicking; commits survive resets for ~90 days.