Git Cheat Sheet
Setup, commits, history, branches, undoing, remotes, rebase and recovery. The Git commands you'll actually use, each with a one-line explanation.
Setup
| Command | What it does | Lesson |
|---|
git --version | Check Git is installed | Introduction |
git config --global user.name "Name" | Set your name | Introduction |
git config --global user.email "you@x.com" | Set your email | Introduction |
git help <command> | Open help | Introduction |
Saving work
| Command | What it does | Lesson |
|---|
git init | Start a repository | Commits |
git status | What's changed and staged | Commits |
git add <file> / git add . | Stage changes | Commits |
git commit -m "Message" | Save a snapshot | Commits |
History
| Command | What it does | Lesson |
|---|
git log --oneline | Short list of commits | History |
git log --oneline --graph --all | Branch diagram | History |
git diff | Unstaged changes | History |
git diff --staged | What you're about to commit | History |
git show <hash> | One commit in detail | History |
.gitignore | Files Git should never track | History |
Configuration & aliases
| Command | What it does | Lesson |
|---|
git config --list --show-origin | Every setting and where it comes from | Config & Aliases |
git config --global init.defaultBranch main | Name new branches main | Config & Aliases |
git config --global core.editor "code --wait" | Choose your editor | Config & Aliases |
git config --global alias.st status | Create a shortcut: git st | Config & Aliases |
git config --global pull.rebase true | Rebase instead of merge on pull | Config & Aliases |
Branches & merging
| Command | What it does | Lesson |
|---|
git branch | List branches | Branches |
git switch -c <name> | Create and switch | Branches |
git switch <name> | Change branch | Branches |
git branch -d <name> | Delete a merged branch | Branches |
git merge <branch> | Merge into current branch | Merging |
git merge --abort | Cancel a merge | Merging |
Merge conflicts
| Command | What it does | Lesson |
|---|
git checkout --ours file | Keep your version | Merge Conflicts |
git checkout --theirs file | Keep the incoming version | Merge Conflicts |
git add file then git commit | Mark resolved and finish | Merge Conflicts |
git config --global merge.conflictStyle zdiff3 | Show the original lines in conflicts | Merge Conflicts |
Undoing
| Command | What it does | Lesson |
|---|
git restore <file> | Discard uncommitted edits | Undoing |
git restore --staged <file> | Unstage | Undoing |
git commit --amend | Fix the last (unpushed) commit | Undoing |
git revert <hash> | Undo a commit with a new commit | Undoing |
git reset --soft HEAD~1 | Remove last commit, keep changes | Undoing |
Stashing
| Command | What it does | Lesson |
|---|
git stash push -m "note" | Shelve changes with a description | Stash |
git stash -u | Include untracked files | Stash |
git stash list | Show all stashes | Stash |
git stash pop | Reapply the latest and remove it | Stash |
git stash apply stash@{1} | Reapply a stash but keep it | Stash |
git stash drop stash@{0} | Delete one stash | Stash |
Remotes
| Command | What it does | Lesson |
|---|
git clone <url> | Copy a remote repository | Remotes |
git remote -v | List remotes and URLs | Remotes |
git remote add origin <url> | Connect a remote | Remotes |
git push -u origin <branch> | Push and set upstream | Remotes |
git fetch | Download without merging | Remotes |
git pull | Fetch and merge | Remotes |
Tags
| Command | What it does | Lesson |
|---|
git tag | List tags | Tags |
git tag -a v1.0.0 -m "Message" | Create an annotated tag | Tags |
git push origin v1.0.0 | Push one tag | Tags |
git push --tags | Push all tags | Tags |
git tag -d v1.0.0 | Delete a local tag | Tags |
Rebase & cherry-pick
| Command | What it does | Lesson |
|---|
git rebase main | Replay your branch on top of main | Rebase |
git rebase -i HEAD~3 | Reword, squash or drop recent commits | Rebase |
git rebase --continue / --abort | Carry on or cancel a rebase | Rebase |
git push --force-with-lease | Safely update your rebased branch | Rebase |
git cherry-pick <hash> | Copy one commit onto this branch | Cherry-Pick |
git cherry-pick -x <hash> | Copy and record the source commit | Cherry-Pick |
Recovery & investigation
| Command | What it does | Lesson |
|---|
git reflog | Every recent move of HEAD | Reflog |
git reset --hard HEAD@{1} | Go back to before the last move | Reflog |
git branch <name> <hash> | Restore a deleted branch | Reflog |
git bisect start / good / bad | Binary search for a bad commit | Bisect & Blame |
git bisect run <command> | Automate the search | Bisect & Blame |
git blame -L 10,20 <file> | Who last changed each line | Bisect & Blame |
git log -S "text" | Commits that added or removed text | Bisect & Blame |
Team workflow
| Command | What it does | Lesson |
|---|
git switch main && git pull | Start from the latest main | Workflows |
git switch -c feature/name | One branch per task | Workflows |
git commit -m "feat: summary" | Conventional Commits message | Workflows |
git branch -d feature/name | Tidy up after merging | Workflows |
Hooks, submodules & worktrees
| Command | What it does | Lesson |
|---|
chmod +x .git/hooks/pre-commit | Enable a hook | Git Hooks |
git config core.hooksPath .githooks | Share hooks with the team | Git Hooks |
git commit --no-verify | Skip commit hooks once | Git Hooks |
git submodule add <url> <path> | Add a submodule | Submodules |
git clone --recurse-submodules <url> | Clone with submodules | Submodules |
git submodule update --remote | Update a submodule | Submodules |
git worktree add ../dir -b branch main | Second folder, new branch | Worktrees |
git worktree list / remove | List / remove worktrees | Worktrees |
How to use this Git cheat sheet
This Git cheat sheet collects the commands you'll use most, grouped in the same order as the course: setup and configuration, saving work, history, managing files, branches, merging and conflicts, undoing, stashing, remotes, tags, rebasing, cherry-picking, recovery, investigation, hooks, submodules and worktrees. Each row has a one-line explanation and a link to the lesson that covers it with full examples and common mistakes.
Keep it open while you work through the lessons or your own projects. When you can't remember whether it's git restore --staged or git reset that unstages a file, or how to include the first commit in a cherry-pick range, a quick look here is faster than a search. Type part of a command into the filter box to jump straight to it, or print the page and keep it beside your keyboard.
Git gotchas worth remembering
- Stage before you commit.
git commit only saves what git add staged. Check with git status first. - reset --hard throws work away. Uncommitted changes can't be recovered. Use
git stash if you might want them back. - Don't rebase shared branches. Rewriting commits others have pulled forces everyone to repair their history. Rebase only your own local work.
Try each command in a throwaway repository before using it on real work, then check yourself with the Git quiz.
The Git lessons behind this cheat sheet