Skip to content

Git cheat sheet

This Git cheat sheet fits the commands you use every day, branching, stashing and 14 undo recipes on one printable page, using the current commands (git switch and git restore) and the safe variants (--force-with-lease, reset --keep) where there is a choice.

Paper

Fits one page: A4, Letter.

Git

Everyday commands, branches, stash and undo recipes

git 2.23+Reviewed 2026-09-27

Start

  • git initCreate a repository in the current folder
  • git clone <url>Copy a remote repository with its full history
  • git config --global user.name "Your Name"Name recorded in your commits
  • git config --global user.email you@example.comEmail recorded in your commits

Everyday

  • git statusChanged, staged and untracked files
  • git add <path>Stage a file or folder for the next commit
  • git add -pStage change by change, reviewing each hunk
  • git commit -m "message"Record the staged changes
  • git diffChanges not staged yet
  • git diff --stagedChanges staged for the next commit (same as --cached)
  • git log --oneline --graph --allCompact history of every branch
  • git show <commit>One commit: message, author and diff
  • git pullFetch and integrate the upstream branch
  • git pushSend your commits to the upstream branch
  • git fetch --pruneUpdate remote branches and drop deleted ones

Branches

  • git branch -aList local and remote-tracking branches
  • git switch -c <branch>Create a branch and switch to it
  • git switch <branch>Switch to an existing branch
  • git switch -Back to the previous branch
  • git merge <branch>Merge that branch into the current one
  • git rebase mainReplay your commits on top of main
  • git push -u origin <branch>Publish a new branch and track it
  • git branch -m <new-name>Rename the current branch
  • git branch -d <branch>Delete a merged branch (-D forces it)
  • git push origin --delete <branch>Delete a branch on the remote
  • git cherry-pick <commit>Copy one commit onto the current branch

Stash

  • git stash -uShelve uncommitted changes, untracked files included
  • git stash listShelved changes, newest first as stash@{0}
  • git stash popReapply the latest stash and drop it
  • git stash apply stash@{1}Reapply a stash but keep it in the list

Undo

  • Discard edits to a filegit restore <file>The uncommitted changes in it are gone for good
  • Unstage a file, keep the editsgit restore --staged <file>Undoes git add for that file
  • Fix the last commit messagegit commit --amend -m "new message"Only before pushing: it replaces the commit
  • Add a forgotten file to the last commitgit add <file> git commit --amend --no-editKeeps the message as it was
  • Undo the last commit, keep changes stagedgit reset --soft HEAD~1Files and index untouched; only the commit goes
  • Undo the last commit, keep changes unstagedgit reset HEAD~1--mixed is the default mode
  • Throw away the last commit and its changesgit reset --hard HEAD~1Also wipes uncommitted work; reflog can still find the commit
  • Undo a commit that is already pushedgit revert <commit>Adds a new commit that reverses it; history stays intact
  • Move the last commit to a new branchgit branch <new> git reset --keep HEAD~1 git switch <new>--keep refuses to run if it would lose local edits
  • Recover a deleted branch or lost commitgit reflog git branch rescue <sha>reflog lists every position HEAD had
  • Get a file back from an older commitgit restore --source=<commit> <file>Overwrites the working copy of that file
  • Stop a merge or rebase that went wronggit merge --abort git rebase --abortBack to the state before it started
  • Delete untracked filesgit clean -n git clean -fd-n lists what would go; -f deletes, -d includes folders
  • Push after rewriting historygit push --force-with-leaseRefuses if the remote moved since your last fetch

Inspect and tag

  • git log -p <file>Every change to one file, with diffs
  • git blame <file>Last commit and author of each line
  • git diff main...<branch>What a branch changed since it forked from main
  • git tag -a v1.2.0 -m "v1.2.0"Annotated tag on the current commit
  • git push origin v1.2.0Tags are not pushed with the branch
Checked against git-scm.com and Git 2.55www.arielton.com/cheatsheets/git

What is on the sheet

The undo block is organized by the problem, not by the command, because nobody remembers whether they need reset, restore or revert: they remember that they committed to the wrong branch. Each recipe says what it keeps and what it destroys, since that is the part that costs work when you get it wrong.

Every option on the sheet was checked against the git-scm.com reference and against the usage output of Git 2.55, and the undo recipes were run in a scratch repository to confirm what they do to the working tree, the index and the branch.

How to print it on one page

Pick the paper above the sheet and press Print / Save as PDF. The page measures the sheet at that paper width and scales it to fill one page, never below 7pt, then hides the site header, footer, ads and buttons. It always prints black on white, whatever theme you are reading in.

In the print dialog keep Scale on Default (100%) and Margins on Default. To get a PDF, choose Save as PDF as the destination. Safari ignores the paper size a page asks for, so select the same paper in its dialog.

Embed or link to this cheat sheet

Linking to this page from your docs, wiki or README is the best way to share it: readers always get the current version and the print button. Copy one of these:

HTML
<a href="https://www.arielton.com/cheatsheets/git">Git cheat sheet (printable)</a> by <a href="https://www.arielton.com/">Arielton Oberek</a>
Markdown
[Git cheat sheet (printable)](https://www.arielton.com/cheatsheets/git) by [Arielton Oberek](https://www.arielton.com/)

If your team wants the same treatment for an internal API, a CLI or a style guide, write to contact@arielton.com.

Frequently asked questions

How do I undo the last commit in Git?
git reset --soft HEAD~1 removes the commit and keeps its changes staged, git reset HEAD~1 keeps them unstaged, and git reset --hard HEAD~1 throws them away. If the commit was already pushed, use git revert <commit> instead so you do not rewrite shared history.
What is the difference between git switch and git checkout?
git switch, added in Git 2.23 together with git restore, only changes branches, and git restore only restores files. git checkout does both, which made it easy to overwrite files by accident. checkout still works; the sheet uses the newer commands.
Is git push --force-with-lease safe?
Safer than --force: it refuses to overwrite the remote branch if it moved since your last fetch, so you do not silently drop a teammate's commits. It still rewrites history, so keep it for branches nobody else builds on.
How do I recover a branch I deleted?
Run git reflog, find the last commit you had on that branch, and recreate it with git branch <name> <sha>. Unreachable commits stay recoverable until garbage collection prunes them, which by default takes at least 30 days.

Last reviewed by Arielton Oberek.