Git commands cheat sheet

The Git commands people actually search for: undoing commits, fixing branches, stashing, rewriting messages and recovering lost work. Fill in the blanks and copy.

37 commands

Undo the last commit, keep the changes

macOSWindowsLinux

Removes the last commit but leaves its changes staged, ready to edit and commit again.

git reset --soft HEAD~1

If the commit was already pushed, use git revert instead so you don’t rewrite shared history.

Safely undo a pushed commit

macOSWindowsLinux

Creates a new commit that reverses an earlier one. Safe on shared branches because history isn’t rewritten.

git revert

Fix the last commit message

macOSWindowsLinuxCareful

Replaces the message of the most recent commit.

git commit --amend -m ""

Rewrites the commit. If it was already pushed you’ll need git push --force-with-lease.

Add a forgotten file to the last commit

macOSWindowsLinuxCareful

Stages the file and folds it into the previous commit without changing its message.

git add && git commit --amend --no-edit

Rewrites the last commit; avoid on commits others have pulled.

Short status with branch info

macOSWindowsLinux

A compact view of changed files plus how far ahead or behind your branch is.

git status -sb

Stage everything and commit

macOSWindowsLinux

Stages new, changed and deleted files, then commits them in one go.

git add -A && git commit -m ""

Create and switch to a new branch

macOSWindowsLinux

Creates a branch from where you are and moves onto it.

git switch -c

Switch branches

macOSWindowsLinux

Moves to an existing branch.

git switch

Push a new branch and track it

macOSWindowsLinux

Publishes the current branch with the same name and sets it as the upstream, so plain git push works next time.

git push -u origin HEAD

Pull without a merge commit

macOSWindowsLinux

Fetches remote changes and replays your local commits on top, keeping history linear.

git pull --rebase

Discard changes to a file

macOSWindowsLinuxCareful

Throws away uncommitted edits to a file and restores the last committed version.

git restore

Uncommitted edits to that file are lost.

Unstage a file

macOSWindowsLinux

Removes a file from the staging area but keeps your edits.

git restore --staged

Make local branch match the remote exactly

macOSWindowsLinuxDestructive

Throws away all local commits and changes on this branch and copies the remote version.

git fetch origin && git reset --hard origin/

Deletes uncommitted work and unpushed commits. Run git stash first if you might need them.

Stash work in progress

macOSWindowsLinux

Shelves your uncommitted changes so you can switch branches with a clean tree.

git stash push -m ""

Bring back stashed work

macOSWindowsLinux

Re-applies the most recent stash and removes it from the stash list.

git stash pop

List stashes

macOSWindowsLinux

Shows every stash with its message.

git stash list

Pretty one-line history graph

macOSWindowsLinux

A compact visual graph of all branches and commits.

git log --oneline --graph --decorate --all

See what you’re about to commit

macOSWindowsLinux

Shows the exact changes that are staged.

git diff --staged

See who changed each line

macOSWindowsLinux

Annotates every line with the commit and author that last changed it.

git blame

Copy one commit onto this branch

macOSWindowsLinux

Applies the changes from a single commit on another branch to your current branch.

git cherry-pick

Recover "lost" commits

macOSWindowsLinux

Shows everywhere HEAD has been, including commits removed by reset or rebase. Use the hash with git reset or git switch -c to get them back.

git reflog

Preview deleting untracked files

macOSWindowsLinux

Lists the untracked files and folders that git clean -fd would delete. Nothing is removed.

git clean -nd

Delete untracked files and folders

macOSWindowsLinuxDestructive

Removes every untracked file and directory from the working tree.

git clean -fd

Permanently deletes untracked files (they’re not in Git, so there’s no undo). Run git clean -nd first to preview.

Delete a local branch

macOSWindowsLinux

Deletes a branch that has been merged. Use -D to force-delete an unmerged one.

git branch -d

Delete a remote branch

macOSWindowsLinuxCareful

Removes the branch from the remote repository.

git push origin --delete

Rename the current branch

macOSWindowsLinux

Renames the branch you’re on.

git branch -m

Delete all merged local branches

macOSLinuxCareful

Cleans up local branches that are already merged into the base branch.

git branch --merged | grep -v '^[ *]*$' | xargs git branch -d

Force-push safely

macOSWindowsLinuxCareful

Overwrites the remote branch only if nobody else has pushed since you last fetched.

git push --force-with-lease

Still rewrites remote history; only use on branches you own.

Squash the last few commits into one

macOSWindowsLinuxCareful

Combines your last N commits into a single commit.

git reset --soft HEAD~ && git commit -m ""

Rewrites history; avoid on commits that are already shared.

Tag and push a release

macOSWindowsLinux

Creates an annotated version tag and publishes it.

git tag -a v -m "Release v" && git push origin v

Set your name and email

macOSWindowsLinux

Sets the author identity used on every commit on this computer.

git config --global user.name "" && git config --global user.email ""

Use "main" for new repositories

macOSWindowsLinux

Makes git init create a main branch instead of master.

git config --global init.defaultBranch main

Change the remote URL

macOSWindowsLinux

Points origin at a new address, for example when switching from HTTPS to SSH.

git remote set-url origin

Clone just the latest snapshot

macOSWindowsLinux

Downloads only the most recent commit. Much faster for big repositories.

git clone --depth 1

Commits per author

macOSWindowsLinux

Ranks contributors by number of commits.

git shortlog -sn --all

Stop tracking a file but keep it

macOSWindowsLinux

Removes the file from Git (for example after adding it to .gitignore) without deleting it from disk.

git rm --cached

Every command copies as plain, clean text: no curly quotes, no long dashes, no invisible characters.

Related guides