Home / Tools / Git Cheat Sheet
Free tool · runs in your browser

Git Cheat Sheet

The Git commands worth knowing, grouped by what you are actually trying to do — including which ones are safe on a shared branch and which will rewrite history other people have already pulled.

Reference

A reference page. Catalase has a visual git panel with staging, diffs and AI-written commit messages — see the features.

How it works

The commands, by task

Everyday work

CommandWhat it does
git statusWhat is staged, changed and untracked
git add -pStage changes hunk by hunk — the best habit on this page
git commit -m "msg"Commit what is staged
git commit --amendRewrite the last commit. Never after pushing to a shared branch
git log --oneline --graph --allReadable history with branch topology
git diff / --stagedUnstaged changes / staged changes
git switch -c nameCreate and move to a branch (clearer than checkout -b)
git restore fileDiscard unstaged changes to a file
git restore --staged fileUnstage, keeping the edit

Undoing things

CommandWhat it doesSafe on a shared branch?
git revert <sha>New commit undoing an old oneYes
git reset --soft HEAD~1Undo the commit, keep changes stagedNo
git reset --mixed HEAD~1Undo the commit, keep changes unstagedNo
git reset --hard HEAD~1Undo the commit and discard the changesNo — destructive
git reflogEvery HEAD position, including "lost" commitsYes
git stash / git stash popShelve and restore work in progressYes

Branches and remotes

CommandWhat it does
git fetch --pruneUpdate remote refs and drop deleted ones
git pull --rebaseUpdate your branch without a merge commit
git merge --no-ff nameMerge, always creating a merge commit
git rebase mainReplay your commits on top of main
git push -u origin namePush and set upstream tracking
git push --force-with-leaseForce push that refuses if someone else pushed first
git cherry-pick <sha>Apply one commit from elsewhere

Finding things

CommandWhat it does
git blame fileWho last changed each line
git log -S"text"Commits that added or removed that text
git log -- fileHistory of one file
git bisect startBinary search for the commit that broke something
git grep "text"Search the working tree, respecting .gitignore

Almost nothing is really lost

The single most useful thing to know: git reflog records every position HEAD has held, including commits that are no longer reachable from any branch. A bad reset --hard, a botched rebase, a deleted branch — all recoverable.

git reflog                  # find the sha you want
git reset --hard <sha>      # or: git branch recovered <sha>

Unreachable commits survive until garbage collection, which by default leaves them for 90 days. The exception, and it is the important one: uncommitted changes are not in Git at all. git reset --hard or git checkout -- destroys them with no recovery. Commit or stash before anything drastic.

Rewriting history, and when not to

commit --amend, rebase and reset all rewrite history: they replace commits with new ones that have different hashes. On your own unpushed work this is good practice — it produces a history worth reading.

On a branch others have pulled, it forces everyone else to reconcile a divergent history, and someone will resolve it wrongly. The rule: rewrite freely before you push, never after, unless the branch is yours alone and you have said so.

When you must force push, use --force-with-lease rather than --force. It refuses if someone else has pushed since you last fetched, which turns silently destroying a colleague’s work into an error message.

Merge or rebase

Both integrate one branch into another; they differ in the history they leave.

  • Merge preserves exactly what happened, at the cost of merge commits. Honest, and sometimes noisy.
  • Rebase replays your commits on top of the target, giving a linear history. Cleaner to read, but it rewrites your commits — so rebase your own feature branch, not a shared one.

A common convention: rebase your feature branch onto main while you work, then merge it with --no-ff so the feature stays visible as a unit.

Writing commit messages worth reading

A short imperative summary under about 50 characters (“Fix off-by-one in pagination”, not “Fixed” or “changes”), a blank line, then the why — the diff already shows the what.

Conventional Commits adds a machine-readable prefix: feat:, fix:, docs:, refactor:, test:, chore:, with ! or a BREAKING CHANGE: footer for incompatible changes. Tooling can then derive changelogs and version bumps automatically.

What people use it for

  • Undoing a commit without losing the work in it.
  • Recovering a branch you deleted or reset away.
  • Finding which commit introduced a bug with bisect or log -S.
  • Cleaning up a messy branch before opening a pull request.
  • Checking whether a command is safe on a branch other people share.

Nothing is uploaded. This tool runs entirely in JavaScript in your browser. Your input is never sent to a server, never logged and never stored, and no account is required. Once the page has loaded you can go offline and it still works — open your browser's network tab and watch if you'd like to confirm it.

Frequently asked questions

How do I undo my last commit?

Depends what you want kept. git reset --soft HEAD~1 undoes the commit and keeps changes staged; --mixed keeps them unstaged; --hard discards them entirely. On a branch others have pulled, use git revert instead, which records a new commit that undoes the old one.

What is the difference between reset and revert?

reset moves the branch pointer and rewrites history, which is unsafe on a shared branch. revert creates a new commit that undoes an earlier one, leaving history intact, which is always safe to push.

Can I recover a commit I reset away?

Almost always. git reflog lists every position HEAD has held, including unreachable commits, and they survive for 90 days by default. Uncommitted changes are the exception: they were never in Git and cannot be recovered.

Should I merge or rebase?

Rebase your own feature branch to keep history linear and readable; merge to integrate it, ideally with --no-ff so the feature stays visible as a unit. Never rebase a branch other people have pulled.

Why use --force-with-lease instead of --force?

It refuses the push if someone else has pushed since you last fetched. Plain --force overwrites their work silently; --force-with-lease turns that into an error you can act on.

Tools that run where your code is

This page does its work in your browser because sending your data to a server to process it is a bad default. Catalase applies the same idea to AI coding: a free, open-source code editor that runs language models on your own machine through Ollama, with no account and no telemetry. Cloud models are there when you choose one, not by default.

Download Catalase freeWhat is Catalase?

Related tools

All 30 free developer tools →