Git Command Cheatsheet

Next

Git has about 150 subcommands and several of them look dangerously similar, reset vs restore, checkout vs switch, rm vs restore --staged. This cheatsheet groups the commands you actually type by situation: daily workflow, branching, undoing mistakes, and stashing. Pick the group and copy the block.

How to use the cheatsheet

  1. 1

    Pick a topic

    Daily, branching, undo or stash: each maps to a different mental mode.

  2. 2

    Scan the block

    Every line is a complete, working command: no placeholders in angle brackets unless explicitly marked.

  3. 3

    Copy the one you need

    Paste into your terminal, adjust the branch name or commit SHA, and run.

Why commands still confuse seasoned users

Git’s porcelain grew by accretion. checkout was overloaded for “switch branch” and “restore file”; reset sits between “discard work” and “move the branch pointer”. Since Git 2.23 there are clearer verbs:

Old New (clearer)
git checkout branch git switch branch
git checkout -b new git switch -c new
git checkout -- file git restore file
git checkout --staged file git restore --staged file

The safe undo ladder

  1. Restore one file: git restore path/to/file (unstaged) or git restore --staged path/to/file (staged but not committed).
  2. Undo the last commit, keep changes: git reset --soft HEAD~1.
  3. Undo the last commit, discard changes: git reset --hard HEAD~1 (destructive; check you have no uncommitted work).
  4. Revert a shared commit: git revert <sha>, creates a new commit that negates the old one; safe on pushed history.

Stash etiquette

  • Always stash with a message: git stash push -m "wip: hooking up api".
  • List stashes with git stash list before popping, anonymous stashes pile up fast.
  • git stash pop applies and deletes; git stash apply applies and keeps.

Recovery

Lost a commit after a hard reset? It is probably still in the reflog. Run git reflog to see every HEAD move of the last 90 days and git reset --hard <sha> back to a good state.

Frequently Asked Questions

Since Git 2.23 (August 2019) the project split the overloaded checkout verb into two. switch changes branches, restore fixes files. Both still exist for backwards compatibility but are easier to read when you revisit the shell history.

reset moves the branch pointer (and optionally the working tree). revert creates a new commit that undoes a previous one, safe on a shared branch. checkout / restore changes files in the working tree without rewriting history.

Use git revert <sha> to add a compensating commit and push that. Avoid git reset --hard + git push --force on shared branches unless the team has explicitly agreed.

The commands are static text rendered in the page. The topic you select is sent to the server only to render the matching commands, and it is carried in the page link when you move between the two steps. It is not stored or logged.

Related Tools

Tool available in other languages