Git Cheat Sheet

The daily commands — and the undo section you're probably actually here for.

Let's start where most people arrive: you did something and you want it undone. Then the normal reference.

Undoing things

SituationFix
Wrong commit message (not pushed)git commit --amend
Forgot a file in the last commitgit add f && git commit --amend --no-edit
Undo last commit, keep the changes stagedgit reset --soft HEAD~1
Undo last commit, keep changes unstagedgit reset HEAD~1
Undo last commit and throw away the workgit reset --hard HEAD~1
Unstage a file (keep edits)git restore --staged file
Discard edits to a filegit restore file
Undo a commit that's already pushedgit revert <sha>
Set something aside temporarilygit stash / git stash pop
The rule that prevents disasters: use reset on commits nobody else has, and revert on anything already pushed. revert makes a new commit that undoes the old one, so shared history stays intact. Force-pushing a rewritten branch other people are working on is how you ruin a colleague's afternoon.
You probably didn't lose it. git reflog lists everywhere HEAD has been — including commits you "deleted." Find the sha and git checkout -b recovered <sha>. This has saved more work than any other Git command.

Daily driver

CommandWhat it does
git statusWhere am I, what's changed
git add -pStage hunk by hunk. Makes clean, reviewable commits instead of one blob.
git commit -m "msg"Commit staged changes
git log --oneline --graph --allReadable history with branch structure
git diffUnstaged changes
git diff --stagedWhat you're about to commit — check before every commit
git blame fileWho wrote each line, and in which commit

Branching

CommandWhat it does
git switch -c featureCreate and switch (modern form of checkout -b)
git switch mainSwitch branches
git branch -d featureDelete merged branch (-D to force)
git merge featureMerge into current branch
git rebase mainReplay your commits on top of main — linear history
git cherry-pick <sha>Grab one commit from elsewhere

Merge vs rebase, briefly: merge preserves what actually happened and is always safe. Rebase produces cleaner, linear history but rewrites commits — fine on your own branch, avoid on shared ones.

Remotes

CommandWhat it does
git clone urlCopy a repo down
git remote -vWhere does this push and pull from
git fetchDownload refs without touching your files — safe anytime
git pullFetch + merge into your branch
git push -u origin featurePush a new branch and set upstream
git push --force-with-leaseSafer force-push — refuses if someone else pushed meanwhile
Never plain --force. Use --force-with-lease. It does the same thing but aborts if the remote moved since you last looked, which is exactly the case where a plain force-push destroys someone's work.

Merge conflicts, without panic

A conflict means Git can't decide between two versions. Open the file, find the <<<<<<< / ======= / >>>>>>> markers, delete the markers and leave the code you want, then git add the file and git commit. If it's going badly, git merge --abort puts everything back and nothing is lost.

Want this as a printable sheet?

The Git & GitHub Cheat Sheet for Beginners — one clean page including the undo table, made to print. Instant download.

Get the sheet — $3 →

Pairs with the Linux commands sheet and the Docker sheet — the other two things open in every terminal.