Git Cheat Sheet — Including the Recovery Commands
Everyday commands, plus the ones you need under pressure: reflog, the three reset modes, and undoing a bad merge.
Setup & Config
git config --global user.name "Name" Set your global username git config --global user.email "email@example.com" Set your global email git config --global core.editor "code --wait" Set VS Code as default editor git config --list List all config settings git config --global alias.st status Create a command alias (st → status) Creating & Cloning
git init Initialize a new local repository git init <directory> Create a new repo in a subdirectory git clone <url> Clone a remote repository locally git clone <url> <directory> Clone into a specific folder name git clone --depth 1 <url> Shallow clone — only latest commit Staging & Committing
git status Show working tree status git add <file> Stage a specific file git add . Stage all changes in current directory git add -p Interactively stage hunks (partial stage) git commit -m "message" Commit staged changes with message git commit --amend Amend the last commit (message or files) git commit --amend --no-edit Amend last commit without editing message git reset HEAD <file> Unstage a file (keep working changes) git restore <file> Discard working directory changes git restore --staged <file> Unstage a file (Git 2.23+) Branching
git branch List local branches git branch -a List all branches (local + remote) git branch <name> Create a new branch git branch -d <name> Delete a branch (safe — must be merged) git branch -D <name> Force delete a branch git branch -m <old> <new> Rename a branch git checkout <branch> Switch to an existing branch git checkout -b <branch> Create and switch to a new branch git switch <branch> Switch branch (Git 2.23+) git switch -c <branch> Create and switch (Git 2.23+) Merging & Rebasing
git merge <branch> Merge a branch into current branch git merge --no-ff <branch> Merge with a merge commit (no fast-forward) git merge --squash <branch> Squash branch commits into one staged change git merge --abort Abort a merge in progress git rebase <branch> Rebase current branch onto another git rebase -i HEAD~n Interactive rebase — squash/edit last n commits git rebase --continue Continue rebase after resolving conflicts git rebase --abort Abort a rebase in progress git cherry-pick <commit> Apply a specific commit to current branch Remote Repositories
git remote -v List remote connections with URLs git remote add origin <url> Add a remote named "origin" git remote remove <name> Remove a remote git remote set-url origin <url> Change remote URL git fetch Download remote changes (no merge) git fetch --prune Fetch and remove stale remote-tracking branches git pull Fetch and merge from tracking branch git pull --rebase Fetch and rebase instead of merge git push origin <branch> Push branch to remote git push -u origin <branch> Push and set upstream tracking git push --force-with-lease Force push (safer — fails if remote changed) git push origin --delete <branch> Delete a remote branch Stashing
git stash Stash current working directory changes git stash push -m "message" Stash with a descriptive name git stash list List all stashes git stash pop Apply most recent stash and remove it git stash apply stash@{n} Apply a specific stash (keep it) git stash drop stash@{n} Delete a specific stash git stash clear Remove all stashes git stash branch <branch> Create a branch from a stash Log & Diff
git log Show commit history git log --oneline --graph --decorate Compact branch graph view git log -p Show commits with diffs git log --author="Name" Filter commits by author git log --since="2 weeks ago" Filter commits by date git log --follow <file> Show file history including renames git diff Show unstaged changes git diff --staged Show staged changes git diff <branch1>..<branch2> Compare two branches git blame <file> Show who changed each line git show <commit> Show commit details and diff Undoing & Reset
git revert <commit> Create a new commit that undoes a commit git revert HEAD Revert most recent commit git reset --soft HEAD~1 Undo last commit — keep changes staged git reset --mixed HEAD~1 Undo last commit — keep changes unstaged git reset --hard HEAD~1 Undo last commit — discard all changes git clean -fd Remove untracked files and directories git reflog Show history of HEAD movements (recovery tool) Tags
git tag List all tags git tag v1.0.0 Create a lightweight tag git tag -a v1.0.0 -m "message" Create an annotated tag git push origin v1.0.0 Push a specific tag to remote git push origin --tags Push all tags to remote git tag -d v1.0.0 Delete a local tag git push origin --delete v1.0.0 Delete a remote tag Advanced
git bisect start Start binary search for a bug-introducing commit git bisect good <commit> Mark commit as working (good) git bisect bad <commit> Mark commit as broken (bad) git worktree add <path> <branch> Check out a branch into a new directory git submodule add <url> Add a git submodule git submodule update --init --recursive Initialize and update all submodules git archive --format zip HEAD > out.zip Export current HEAD as a ZIP git shortlog -sn Commit count per author Undoing things — the table you actually need
Ranked by how much they destroy. Check which one you want before running it.
| Goal | Command | Destroys work? |
|---|---|---|
| Unstage a file | git restore --staged FILE | No |
| Discard uncommitted changes to a file | git restore FILE | Yes — unrecoverable |
| Amend the last commit message | git commit --amend | No, but rewrites history |
| Undo last commit, keep changes staged | git reset --soft HEAD~1 | No |
| Undo last commit, keep changes unstaged | git reset HEAD~1 | No |
| Undo last commit, discard changes | git reset --hard HEAD~1 | Yes |
| Undo a pushed commit safely | git revert COMMIT | No — creates an inverse commit |
| Recover a "lost" commit | git reflog then git reset --hard SHA | No |
| Discard all local changes | git reset --hard && git clean -fd | Yes — including untracked files |
| Abandon a merge in progress | git merge --abort | No |
| Abandon a rebase in progress | git rebase --abort | No |
Use revert, not reset, on anything already pushed to a shared branch. Reset rewrites history and forces everyone else to recover their own clone.
Commands worth adding to your vocabulary
- git switch / git restore —Modern replacements for the overloaded git checkout, which did two unrelated jobs. switch changes branches; restore changes files.
- git stash -u —Stashes untracked files too. Plain stash leaves them behind, which is why a "clean" stash sometimes still breaks the build.
- git log --oneline --graph --all —The fastest way to understand branch topology. Worth an alias.
- git bisect —Binary search through history to find the commit that introduced a bug. Automatable with `git bisect run
` — it will find the culprit unattended. - git blame -w -C —-w ignores whitespace changes and -C follows code moved between files, which makes blame useful rather than a list of reformatting commits.
- git add -p —Stage individual hunks rather than whole files. The single most effective habit for producing reviewable commits.
- git commit --fixup SHA —Marks a commit as a fix for an earlier one; `git rebase -i --autosquash` then folds it in automatically.
- git push --force-with-lease —Like --force, but refuses if someone else has pushed since you last fetched. There is no reason to ever use plain --force instead.
About
The Git Cheatsheet organises the most frequently used Git commands into logical sections: repository setup, staging and committing, branching and merging, rebasing, remote operations, stashing, log and diff, and undoing changes. Click any command to copy it instantly, or use the search bar to find a specific operation.
How to use
- 1 Browse commands by section or use the search bar to find a specific operation.
- 2 Click any command card to copy it to your clipboard.
- 3 Use it alongside the Regex Checker or any coding task for quick lookups.
- What is the difference between git merge and git rebase?
- git merge combines two branches by creating a new merge commit that has two parents, preserving the full history of both branches. git rebase moves or replays your branch's commits on top of another branch, producing a linear history with no merge commit. Use merge for shared/public branches (it preserves context); use rebase to clean up local work before sharing it.
- How do I undo the last commit without losing my changes?
- Run git reset --soft HEAD~1. This moves the HEAD pointer back by one commit but leaves your changes staged in the index, so you can amend them and recommit. Use --mixed (default) to unstage the changes but keep them in the working directory. Use --hard only if you want to permanently discard all changes from the last commit.
- What is git stash and when should I use it?
- git stash temporarily saves your uncommitted changes (staged and unstaged) and reverts the working directory to the last commit. Use it when you need to switch branches or pull updates but are not ready to commit your current work. Run git stash pop to restore the saved changes after switching back, or git stash list to see all stashed entries.
More in Reference & Data
See all reference & data.