Git Cheat Sheet

The Git commands you use every day, setup, the add/commit/push workflow, branching and merging, working with remotes, and undoing mistakes safely.

The Git commands you reach for every day, from your first commit to fixing mistakes. Replace BRANCH, FILE, and URL with your own values.

Setup & starting a repo

CommandWhat it does
git config --global user.name "Name"Set your commit name (and user.email)
git initCreate a new repo in the current folder
git clone URLCopy a remote repo locally

Everyday workflow

CommandWhat it does
git statusShow changed, staged, and untracked files
git add FILEStage a file (git add . stages everything in the current folder)
git commit -m "message"Commit staged changes
git commit -am "message"Stage tracked files and commit in one step
git diffShow unstaged changes (--staged for staged)
git log --oneline --graphCompact, visual commit history

Branching & merging

CommandWhat it does
git branchList branches (-a includes remotes)
git switch -c BRANCHCreate and switch to a new branch
git switch BRANCHSwitch branches (older: git checkout)
git merge BRANCHMerge a branch into the current one
git rebase BRANCHReplay your commits on top of another branch
git branch -d BRANCHDelete a merged branch

Remotes: push & pull

CommandWhat it does
git remote -vList configured remotes
git fetchDownload remote changes without merging
git pullFetch and integrate the upstream branch (fast-forward only by default; --rebase or --no-rebase if diverged)
git push -u origin BRANCHPush and set the upstream branch

Undoing things

CommandWhat it does
git restore FILEDiscard unstaged changes to a file
git restore --staged FILEUnstage a file (keep the changes)
git commit --amendEdit the most recent commit
git reset --hard HEAD~1Delete the last commit and its changes, and discard all uncommitted changes in the working tree and index (destructive)
git revert COMMITMake a new commit that undoes an old one
git stash / git stash popShelve changes, then restore them

Lost? git reflog lists recent states HEAD has pointed to (local, entries expire), so you can usually recover commits that seem gone. When in doubt, commit first, committed work is hard to truly lose.

What this does

A Git cheat sheet lists the commands you use every day — setup, the add/commit/push workflow, branching and merging, remotes, and undoing mistakes.

How to use it

  1. Browse by workflow.
  2. Find the command you need.
  3. Copy it with one tap.
  4. Swap in your branch or remote names.

Example

git commit -m "message" records your staged changes.

Last updated .

Frequently asked questions

How do I undo my last commit?

To keep the changes, use git reset --soft HEAD~1. To discard them entirely, git reset --hard HEAD~1 (destructive: it also throws away any uncommitted changes in your working tree and index). To undo a pushed commit safely, use git revert.

What’s the difference between merge and rebase?

Merge combines branches with a merge commit (or a fast-forward when possible), preserving history. Rebase replays your commits on top of another branch for a linear history. Avoid rebasing commits you have already shared.

I think I lost a commit, can I recover it?

Usually yes. git reflog lists the recent states HEAD has pointed to in your local repo, so you can find the commit’s hash and check it out or reset to it.