Skip to main content

2026-02-18

7 min

Updated September 14, 2026

By Formatho Editorial

Git Commands You Should Know by Heart

GitVersion ControlDev Tools
Markdown documentation on laptop screen

The everyday loop

Ninety percent of Git work is five commands: status to see where you are, add to stage, commit to record, pull --rebase to sync, and push to publish. The other ten percent — the part this guide covers — is recovering from mistakes and understanding what's actually staged.

Staging precisely

  • git add -p — stage hunks interactively; split one file's changes into multiple commits
  • git restore --staged file — unstage without touching the working tree
  • git restore file — discard uncommitted changes in one file (destructive)
  • git stash -u — stash including untracked files; git stash pop to restore

Undoing safely

The key question before any undo: have you pushed? If yes, prefer git revert <sha> — it creates a new commit that inverses the old one, keeping history honest for everyone. If the mistake is still local, you can rewrite:

  • git commit --amend — fix the last commit's message or contents
  • git reset --soft HEAD~1 — undo last commit, keep changes staged
  • git reset --hard HEAD~1 — undo last commit and delete the changes
  • git reflog — every HEAD movement for 90 days; the way back from "disasters"

Branching and inspection

  • git switch -c feature/x — create and switch (the modern checkout -b)
  • git log --oneline --graph --all — the shape of history
  • git diff main...HEAD — your branch vs where it forked
  • git blame -L 20,30 file — who last touched those lines and in which commit
  • git bisect start — binary-search history for the commit that introduced a bug

Two habits worth building

Commit messages in the imperative: "add retry loop", not "added retry loop" — the convention that makes git rebase auto-squash messages read correctly. Never rewrite public history: push --force on a shared branch destroys teammates' work; if you must, use --force-with-lease, which refuses when someone pushed before you.

Keep the full searchable reference open in the Git Cheat Sheet — it runs entirely in your browser.

Formatho Editorial — written and maintained by the team behind formatho.com, a library of free, privacy-first developer tools that run entirely in your browser. Every guide is tested against the tools it describes. Corrections and suggestions: github.com/formatho.