Git Cheat Sheet & Command Generator
Browse, search, and copy Git commands by category.
What is a Git Cheat Sheet?
Git is the distributed version-control system used by nearly every software project to track changes, branch work, and collaborate without overwriting one another. A Git cheat sheet is a quick reference to the commands you reach for most often, so you do not have to memorise every flag or dig through the manual each time you need to undo a commit or push a branch.
This page is an interactive command reference rather than a static wall of text. Around seventy commands are organised into eight categories, and each one appears as a card showing the command itself, a plain-English description, any arguments it accepts, and a copy-ready example. The categories are Setup & Config, Basic Commands, Branch & Merge, Stash, Remote & Sync, Undo & Restore, Tags, and Advanced.
You can search across command names, descriptions, and examples as you type, or narrow the list to a single category with the dropdown. Every card has a copy button that puts the command on your clipboard, and a Use link that drops that command into the search box to surface related entries. The Clear button resets both the search text and the category filter at once.
The whole reference runs in your browser and nothing is installed or sent anywhere, so it works just as well as a bookmark while you are working in a terminal and need a reminder of the exact syntax. Because the commands follow the standard Git interface, everything you copy here runs unchanged in any shell.
Key Features of This Cheat Sheet
- Around seventy commands — from initial setup to advanced recovery, all in one place.
- Eight categories — setup, basic, branch and merge, stash, remote, undo, tags, and advanced.
- Instant search — results filter as you type across names, descriptions, and examples.
- Category filter — focus on just the area you are working in.
- One-click copy — each card copies its command straight to the clipboard.
- Use links — jump from a command to similar ones with a single click.
How to Use
- Browse commands — Scroll through the command cards grouped by category.
- Search — Type in the search box to filter commands by name, description, or example.
- Filter by category — Use the dropdown to show commands from a specific category.
- Copy — Click the copy icon on any card to copy the command to your clipboard.
- Find similar commands — Click Use on a card to drop that command into the search box and reveal related entries.
Git Command Reference
The table below lists the commands most people need, with a short description and the category each belongs to. Use it as a starting point, then copy the full form from the cards above.
| Command | What it does | Category |
|---|---|---|
git init | Create a new repository | Setup & Config |
git clone | Copy a remote repository locally | Setup & Config |
git add | Stage changes for the next commit | Basic Commands |
git commit -m | Record staged changes with a message | Basic Commands |
git status | Show the state of the working tree | Basic Commands |
git log --oneline | Show a compact commit history | Basic Commands |
git diff | Compare changes between revisions | Basic Commands |
git branch | List, create, or delete branches | Branch & Merge |
git switch | Move to another branch | Branch & Merge |
git merge | Combine another branch into the current one | Branch & Merge |
git rebase | Reapply commits on top of a new base | Branch & Merge |
git stash | Set work aside without committing | Stash |
git fetch | Download changes without merging them | Remote & Sync |
git pull | Fetch and integrate remote changes | Remote & Sync |
git push | Publish local commits to a remote | Remote & Sync |
git reset --soft HEAD~1 | Undo the last commit, keep changes staged | Undo & Restore |
git restore --staged | Unstage a file | Undo & Restore |
git revert | Undo a commit by creating a new one | Undo & Restore |
git tag -a | Create an annotated release tag | Tags |
git cherry-pick | Copy a single commit onto the current branch | Advanced |
git bisect | Find the commit that introduced a bug | Advanced |
git reflog | Recover commits that seem to be lost | Advanced |
Worked Examples
- Start a repository —
git init, thengit add ., thengit commit -m "Initial commit". - Build a feature on a branch —
git switch -c feature/login, commit your work, thengit push -u origin feature/login. - Undo the last commit but keep the changes —
git reset --soft HEAD~1rewinds the commit and leaves everything staged. - Fix a commit that is already public —
git revert abc123adds a new commit that reverses the old one without rewriting history. - Shelve unfinished work —
git stash push -m "WIP: sidebar", switch branches, then bring it back withgit stash pop. - Recover a lost commit —
git refloglists recent HEAD positions, and you can return to one withgit reset --hard. - Keep a fork in sync —
git fetch upstreamfollowed bygit rebase upstream/mainreplays your commits on the latest upstream code.
Tips and Common Mistakes
- Fetch before you pull —
git fetchlets you inspect incoming changes, whilegit pullmerges immediately and can surprise you with conflicts. - Know your reset modes —
--softkeeps changes staged,--mixedkeeps them in the working tree, and--harddeletes them. Only the last is destructive. - Revert shared history — once a commit has been pushed, prefer
git revertovergit resetso teammates are not left with broken history. - Force with lease — reach for
git push --force-with-leaseinstead of--force; it refuses to overwrite work you have not seen. - Prefer switch and restore — modern Git splits the overloaded
checkoutintogit switchfor branches andgit restorefor files. - Commit in small pieces — smaller, focused commits are easier to review, revert, and bisect than one large commit at the end of the day.
- Stash is temporary — a stash is not a branch; if work needs to last more than a moment, commit it on a branch instead.
Common Use Cases
A cheat sheet earns its place when you need the right command quickly. These are the moments it helps most.
- Learning Git — beginners can scan commands by category and copy working examples instead of guessing at syntax.
- Day-to-day development — stage, commit, branch, and push without leaving your workflow to read the manual.
- Onboarding new teammates — a shared reference gives everyone the same vocabulary for branches, remotes, and undo.
- Recovering from mistakes — reset, revert, and reflog are the commands people need most urgently and remember least.
- Release management — tagging and pushing tags are covered alongside the everyday work that leads up to a release.
- Debugging history — bisect and blame help track down when a bug appeared and who last touched a line.
Frequently Asked Questions
What is the difference between git checkout and git switch?
git switch is the modern command, introduced in Git 2.23, that only handles moving between branches, which makes its purpose obvious at a glance. git checkout still works and is more versatile, but it is also used to restore files, so a single command can either change branches or overwrite your work. Most people now use git switch for branches and git restore for files.
How do I undo a git commit?
The right command depends on whether the commit has been shared. To undo the most recent local commit but keep the changes staged, run git reset --soft HEAD~1; to discard the changes entirely, run git reset --hard HEAD~1. If the commit has already been pushed, use git revert instead, because that adds a new commit rather than rewriting history.
What is the difference between git pull and git fetch?
git fetch downloads new commits from the remote but leaves your working branch untouched, so you can review what arrived before integrating it. git pull performs a fetch and then immediately merges those changes into your current branch, or rebases onto them if you pass the --rebase flag. Fetching first is the safer habit because it never surprises you with a merge.
What does git reset --hard do?
git reset --hard moves your branch pointer to the target commit and then discards every change in the working tree and staging area so they match that commit exactly. It is powerful but destructive: uncommitted work is deleted with no prompt. If you are unsure of the current state, stash your changes first or use --soft or --mixed instead.
When should I use git revert instead of git reset?
Use git revert whenever the commit you want to undo has been pushed to a shared branch. Revert creates a brand-new commit that reverses the earlier change, so history stays intact and nobody else has to force-pull. git reset rewrites history, which is fine for commits that only exist on your machine but breaks everyone else when used on shared branches.
What is the difference between git merge and git rebase?
git merge combines two branches and records a merge commit, preserving the true branching history exactly as it happened. git rebase instead replays your commits on top of another branch, producing a clean, linear history but creating new commit hashes. As a rule of thumb, merge to integrate shared branches and rebase to tidy up local work before pushing.
How do I recover a commit that seems lost?
The reflog records every position HEAD has held, including commits that are no longer reachable from any branch. Run git reflog to find the short hash of the commit you want back, then reset the branch to that hash or create a new branch pointing at it. Because the reflog is local, act on it on the same machine where the work was lost.
What is the difference between git push --force and --force-with-lease?
git push --force overwrites the remote branch with your local version, even if someone else has pushed commits you have never seen, which can destroy their work. git push --force-with-lease is the safer option: it only succeeds if the remote is still exactly where you last saw it, so it fails rather than clobber a teammate. Prefer force-with-lease whenever you truly need to rewrite a shared branch.
Related Developer Tools
Git Log Formatter
Format and visualize git logs
Changelog Generator
Generate CHANGELOG.md from commits
Diff Checker
Compare text and code side by side
Slug Generator
Convert text to URL-friendly slugs
Regex Tester
Test regular expressions live
.gitignore Generator
Generate .gitignore files