---
title: "Git Commands Cheatsheet"
slug: "git-commands"
category: "cheatsheets"
tags: ["cheatsheets", "git", "reference", "version-control", "cli"]
status: "stable"
last_updated: 2026-05-14
summary: "Common git commands grouped by intent: inspect, undo, branch, sync, rebase, stash, recover. The flags that matter and the ones that bite."
related:
  [
    "[[coding/git]]",
    "[[tooling/github]]",
    "[[cheatsheets/bash-one-liners]]",
    "[[tooling/claude-code-workflow]]",
    "[[cheatsheets/index|Cheatsheets]]",
  ]
---

## Overview

This card collects the git commands worth memorizing, grouped by intent. The full reference is `git help <command>`; this page is the one to skim when you need the right invocation in under ten seconds. For the principles behind these commands (rebase vs merge, conventional commits, fixups), see [[coding/git]].

## Inspect

Read state before you change it. These are the read-only commands.

| Command                           | What it does                                                    |
| --------------------------------- | --------------------------------------------------------------- |
| `git status -sb`                  | Short branch summary; one line per file.                        |
| `git log --oneline -20`           | Last 20 commits, terse.                                         |
| `git log --oneline --graph --all` | Branch topology across all refs.                                |
| `git log -p <file>`               | Full diff history for one file.                                 |
| `git log -S "needle"`             | Commits that added or removed the literal "needle".             |
| `git log --grep "pattern"`        | Commits whose message matches the pattern.                      |
| `git blame -L 10,20 <file>`       | Annotate lines 10 to 20 with the last commit that touched each. |
| `git diff`                        | Unstaged changes.                                               |
| `git diff --staged`               | Changes already in the index.                                   |
| `git diff main...HEAD`            | What this branch adds on top of `main`.                         |
| `git show <ref>`                  | Show a commit, tag, or tree object.                             |
| `git reflog`                      | Local history of HEAD moves; the recovery log.                  |

Reach for `git log --oneline --graph --all` when a teammate says "my branch is gone." It rarely is.

## Undo

Choose the undo that matches the scope. The wrong one rewrites public history.

| Command                              | What it undoes                                                        |
| ------------------------------------ | --------------------------------------------------------------------- |
| `git restore <file>`                 | Discard unstaged changes in working tree.                             |
| `git restore --staged <file>`        | Unstage; leave working tree alone.                                    |
| `git restore --source=HEAD~1 <file>` | Roll one file back to the previous commit.                            |
| `git commit --amend`                 | Replace the tip commit. Local only.                                   |
| `git reset --soft HEAD~1`            | Undo the last commit; keep changes staged.                            |
| `git reset --mixed HEAD~1`           | Undo the last commit; keep changes unstaged.                          |
| `git reset --hard HEAD~1`            | Undo the last commit; throw the work away.                            |
| `git revert <ref>`                   | Create a new commit that inverts an old one. Safe on shared branches. |
| `git clean -fd`                      | Delete untracked files and directories. No undo.                      |

Never `--amend` or `reset` a commit that has been pushed to a shared branch. Use `revert` there.

## Branch

Branches are pointers. Treat them like cheap bookmarks.

| Command                                  | Effect                                        |
| ---------------------------------------- | --------------------------------------------- |
| `git switch <branch>`                    | Move HEAD; modern replacement for `checkout`. |
| `git switch -c <branch>`                 | Create and switch in one step.                |
| `git switch -c <branch> origin/<branch>` | Track a remote branch.                        |
| `git branch -d <branch>`                 | Delete a merged branch.                       |
| `git branch -D <branch>`                 | Delete a branch regardless of merge state.    |
| `git branch --merged main`               | List branches fully merged into `main`.       |
| `git branch -m old new`                  | Rename a branch.                              |
| `git worktree add ../wt branch`          | Check out a second working copy.              |

Prefer `switch` and `restore` over the older `checkout`. They do not overload one verb for two operations.

## Sync

Fetch, then decide. `pull` is `fetch` plus a merge or rebase; the merge surprises people.

| Command                       | Effect                                                  |
| ----------------------------- | ------------------------------------------------------- |
| `git fetch --all --prune`     | Update remote-tracking refs; drop deleted ones.         |
| `git pull --ff-only`          | Refuse to merge; abort if a fast-forward is impossible. |
| `git pull --rebase`           | Replay local commits on top of the remote tip.          |
| `git push -u origin <branch>` | Push and set upstream.                                  |
| `git push --force-with-lease` | Force-push but refuse if the remote moved.              |

Configure `pull.ff = only` globally; require explicit intent when a merge is wanted.

## Rebase, stash, recover

Rewrite history locally; rescue work from the reflog.

| Command                           | Effect                                                 |
| --------------------------------- | ------------------------------------------------------ |
| `git rebase main`                 | Replay this branch on top of `main`.                   |
| `git rebase -i HEAD~5`            | Edit the last 5 commits (squash, reorder, drop).       |
| `git commit --fixup=<ref>`        | Mark a commit as fixup for `<ref>`; autosquash later.  |
| `git rebase -i --autosquash main` | Apply fixups automatically.                            |
| `git stash push -m "wip"`         | Stash with a label.                                    |
| `git stash list`                  | List stashes.                                          |
| `git stash pop`                   | Apply the top stash and drop it.                       |
| `git reflog`                      | Show every HEAD move; the lifeline for "lost" commits. |
| `git reset --hard <reflog-ref>`   | Resurrect a discarded commit.                          |

The reflog keeps entries for 90 days by default. Almost nothing in git is permanently lost.

## Common gotchas

- `git pull` defaults to a merge; configure `pull.ff = only` or `pull.rebase = true` so it cannot create a surprise merge commit.
- `git reset --hard` discards uncommitted work without confirmation. Run `git stash` first if you might want it back.
- `git push --force` overwrites the remote even if a teammate pushed since you fetched. Use `--force-with-lease`.
- `git rebase` of a branch already pushed to a shared remote forces collaborators to recover. Rebase locally; merge after pushing.
- `git clean -fd` deletes untracked files including `.env`. Run `git clean -nd` first to see what it would remove.

## Related

- [[coding/git]]
- [[tooling/github]]
- [[cheatsheets/bash-one-liners]]
- [[tooling/claude-code-workflow]]
- [[cheatsheets/index|Cheatsheets]]
