← All cheatsheets

Git Cheatsheet

A practical Git cheatsheet: the everyday commands plus the rescue moves (reset, reflog, stash, rebase, bisect) — each copy-ready with editable placeholders. Destructive commands are clearly flagged.

Explain a command

Inspect

  • Status (short)
    git status -sb
  • Pretty history graph
    git log --oneline --graph --decorate --all
  • History of one file
    git log --oneline --
    path = file to trace
  • Show what is staged
    git diff --staged
  • Who changed each line
    git blame
  • Show a commit
    git show

Stage & commit

  • Stage everything
    git add -A
  • Stage interactively (hunk by hunk)
    git add -p
  • Commit with a message
    git commit -m ""
    message = commit message
  • Amend the last commitdestructive
    git commit --amend

    Rewrites the last commit — avoid after pushing to a shared branch.

  • Add staged changes to last commit (keep message)destructive
    git commit --amend --no-edit

Branches

  • List branches
    git branch -a
  • Create and switch to a branch
    git switch -c
  • Switch to an existing branch
    git switch
  • Delete a branch (safe)
    git branch -d
  • Force-delete a branchdestructive
    git branch -D
  • Rename the current branch
    git branch -m

Sync with remote

  • Fetch all remotes (prune stale)
    git fetch --all --prune
  • Pull with rebase (linear history)
    git pull --rebase
  • Push and set upstream
    git push -u origin
  • Force-push safely (after rebase)destructive
    git push --force-with-lease

    Refuses to overwrite work you have not seen — prefer over --force.

Stash

  • Stash changes (incl. untracked)
    git stash -u
  • Re-apply the latest stash
    git stash pop
  • List stashes
    git stash list

Undo & rescue

  • Discard changes to a filedestructive
    git restore
  • Unstage a file (keep changes)
    git restore --staged
  • Undo last commit, keep changes staged
    git reset --soft HEAD~
    n = commits to undo
  • Discard all local changes to a refdestructive
    git reset --hard

    Throws away commits and working-tree changes — unrecoverable via normal means.

  • Revert a commit (safe, makes a new commit)
    git revert
  • Find a "lost" commit
    git reflog

    Every HEAD move is here — recover work after a bad reset/rebase.

  • Delete untracked files & dirsdestructive
    git clean -fd

    Permanently removes untracked files. Dry-run first with -n.

Rebase & bisect

  • Interactive rebase (squash/reorder)destructive
    git rebase -i HEAD~

    Rewrites history — only on branches nobody else has pulled.

    n = commits back
  • Rebase current branch onto anotherdestructive
    git rebase
  • Apply a specific commit here
    git cherry-pick
  • Binary-search for a bad commit
    git bisect start
    bad = known-bad ref · good = known-good ref

35 commands. Copy-ready with editable placeholders — everything runs in your browser, nothing is sent to a server.