What is the Git Cheatsheet?
The Git Cheatsheet is a searchable quick reference of the most-used git commands, grouped by task: setup, status, branching, committing, remotes, merging, rebasing, undo, stash, and recovery. Type a keyword to filter, then click any command to copy it to your clipboard. Everything runs in your browser.
Git Cheatsheet
Quick reference for the most-used git commands. Searchable, copy-friendly.
TLDR
Searchable reference of common git commands grouped by use case: setup, status, branching, committing, merging, rebasing, remotes, undo. Click any command to copy.
About this git cheatsheet
Git is the distributed version control system created by Linus Torvalds in 2005 to manage the Linux kernel. It tracks every change to your files as a chain of commits, each identified by a SHA-1 hash, so you can branch, experiment, and roll back without fear. This cheatsheet groups the commands you reach for daily into ten task areas and lets you copy any one with a click.
The commands are pulled from the official git documentation and reflect the modern porcelain commands. In particular it favours git switch and git restore, introduced in git 2.23 (2019), over the older overloaded git checkout, because separating "change branch" from "discard file changes" makes intent clearer and mistakes rarer.
The three areas every commit moves through
Almost every git command makes sense once you picture the three places a change can live. A file moves from one to the next as you stage and commit it.
Working tree -> Staging area (index) -> Repository (commits) edit git add git commit
- Working tree: the files on disk you are editing right now.
git statusshows what changed here. - Staging area: the snapshot you are building for the next commit.
git addmoves changes in;git restore --stagedmoves them back out. - Repository: the committed history.
git commitrecords the staged snapshot permanently;git logshows it.
Worked example: a typical feature branch
The everyday loop for shipping a small change looks like this:
git switch -c feature-login # branch off main git add . # stage your edits git commit -m "add login form" # record them git push -u origin feature-login# publish the branch # open a pull request, get it reviewed and merged git switch main # back to main git pull # fetch the merged work
If you commit too early, git commit --amend folds new staged changes into the last commit (only before you push). If you started on the wrong branch, git stash, switch, then git stash pop moves your uncommitted work across cleanly.
Undo and recovery: the safe vs dangerous commands
| Command | What it does | Safety |
|---|---|---|
git restore <file> | Discard unstaged edits to a file | Loses local edits |
git reset --soft HEAD~1 | Undo last commit, keep changes staged | Safe, reversible |
git revert <hash> | New commit that undoes an old one | Safe after push |
git reset --hard HEAD~1 | Undo commit AND delete changes | Destructive |
git reflog | List every HEAD position to recover lost work | Read-only safety net |
Common mistakes and pitfalls
- Amending or rebasing pushed commits. Rewriting history that teammates have pulled forces a painful re-sync. Only amend or rebase commits that live solely on your machine.
- Using reset --hard as a casual undo. It permanently deletes uncommitted work. Reach for
git restoreorgit stashfirst, and remembergit reflogcan rescue a wrongly reset commit. - Confusing git pull with git fetch.
fetchdownloads remote changes without touching your branch;pullfetches and immediately merges. Use fetch when you want to inspect before integrating. - Committing secrets. Once an API key is committed and pushed, it lives in history forever. Add it to
.gitignorebefore the first commit, and rotate any key that slips through. - Force-pushing to a shared branch.
git push --forcecan erase others' commits. Prefer--force-with-lease, which refuses to overwrite work you have not seen. - Letting branches drift. A feature branch that lags main for weeks becomes a merge nightmare. Rebase or merge main in regularly to keep conflicts small.
Frequently asked questions
How do I undo the last git commit?
Use git reset --soft HEAD~1 to undo the last commit while keeping your changes staged, or git reset --mixed HEAD~1 to keep them unstaged. If the commit was already pushed and shared, use git revert HEAD instead, which adds a new commit that reverses the changes without rewriting history.
What is the difference between git merge and git rebase?
git merge combines two branches and creates a merge commit, preserving the full branching history. git rebase replays your commits on top of another branch, producing a linear history with no merge commit. Rebase is cleaner for local work, but never rebase commits that others have already pulled.
How do I recover a deleted branch or lost commit?
Run git reflog to list every position HEAD has pointed to, including commits from a deleted branch. Find the commit hash you want and run git checkout <hash> or git branch <name> <hash> to restore it. The reflog is git's safety net and keeps entries for about 90 days by default.
What does git stash do?
git stash saves your uncommitted changes to a temporary stack and reverts your working tree to a clean state, so you can switch branches or pull without committing half-finished work. Run git stash pop to reapply the most recent stash and remove it, or git stash list to see all saved stashes.
When should I use git reset --hard?
Use git reset --hard only when you are certain you want to discard uncommitted changes and move the branch pointer, because it permanently deletes working-tree edits. Prefer git restore or git stash for everyday undo. If you reset --hard by mistake, git reflog can often recover the lost commit.
