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 all)
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 merge the remote branch
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 (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 every state HEAD has pointed to, 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.

Sources & methodology

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). To undo a pushed commit safely, use git revert.

What’s the difference between merge and rebase?

Merge combines branches with a merge commit, 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 every state HEAD has pointed to, so you can find the commit’s hash and check it out or reset to it.