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
| Command | What it does |
|---|---|
git config --global user.name "Name" | Set your commit name (and user.email) |
git init | Create a new repo in the current folder |
git clone URL | Copy a remote repo locally |
Everyday workflow
| Command | What it does |
|---|---|
git status | Show changed, staged, and untracked files |
git add FILE | Stage 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 diff | Show unstaged changes (--staged for staged) |
git log --oneline --graph | Compact, visual commit history |
Branching & merging
| Command | What it does |
|---|---|
git branch | List branches (-a includes remotes) |
git switch -c BRANCH | Create and switch to a new branch |
git switch BRANCH | Switch branches (older: git checkout) |
git merge BRANCH | Merge a branch into the current one |
git rebase BRANCH | Replay your commits on top of another branch |
git branch -d BRANCH | Delete a merged branch |
Remotes: push & pull
| Command | What it does |
|---|---|
git remote -v | List configured remotes |
git fetch | Download remote changes without merging |
git pull | Fetch and merge the remote branch |
git push -u origin BRANCH | Push and set the upstream branch |
Undoing things
| Command | What it does |
|---|---|
git restore FILE | Discard unstaged changes to a file |
git restore --staged FILE | Unstage a file (keep the changes) |
git commit --amend | Edit the most recent commit |
git reset --hard HEAD~1 | Delete the last commit and its changes (destructive) |
git revert COMMIT | Make a new commit that undoes an old one |
git stash / git stash pop | Shelve 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
- Browse by workflow.
- Find the command you need.
- Copy it with one tap.
- 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.
Related tools
Docker Cheat Sheet
A reference for everyday Docker, building and tagging images, running and managing containers, ports and volumes, system cleanup, and Docker Compose.
Vim Cheat Sheet
A reference for the Vim editor, modes, moving around, editing, search and replace, and saving or quitting, including the escape hatch for when you are stuck.
Regex Cheat Sheet
A regular-expression reference, character classes, anchors, quantifiers, groups and alternation, lookarounds, common ready-made patterns, and flags.
SQL Cheat Sheet
A reference for standard SQL, querying with SELECT, filtering with WHERE, aggregates and GROUP BY, joining tables, and modifying data and schema.