Git Cheat Sheet
Essential Git commands organized by workflow stage with instant search and copy.
Setup & Config
6 commandsgit config --global user.name "Name"Set global username
git config --global user.email "email"Set global email
git config --listList all config settings
git initInitialize a new local repository
git clone <url>Clone a remote repository
git clone <url> --depth 1Shallow clone (latest commit only)
Staging & Committing
10 commandsgit statusShow working tree status
git add <file>Stage a specific file
git add .Stage all changes
git add -pInteractively stage hunks
git commit -m "message"Commit with a message
git commit --amendAmend the last commit
git diffShow unstaged changes
git diff --stagedShow staged changes
git reset HEAD <file>Unstage a file
git restore <file>Discard working tree changes
Branching & Merging
10 commandsgit branchList local branches
git branch -aList all branches (local + remote)
git branch <name>Create a new branch
git checkout <branch>Switch to a branch
git checkout -b <branch>Create and switch to new branch
git switch -c <branch>Create and switch (modern syntax)
git merge <branch>Merge a branch into current
git rebase <branch>Rebase current onto branch
git branch -d <branch>Delete a merged branch
git branch -D <branch>Force-delete a branch
Remote
8 commandsgit remote -vList remotes with URLs
git remote add origin <url>Add a remote named origin
git fetchFetch all remote changes (no merge)
git pullFetch and merge remote changes
git pull --rebaseFetch and rebase instead of merge
git push origin <branch>Push branch to remote
git push -u origin <branch>Push and set upstream
git push --force-with-leaseSafe force push
Stash
6 commandsgit stashStash current changes
git stash popApply and remove latest stash
git stash listList all stashes
git stash apply stash@{n}Apply a specific stash
git stash drop stash@{n}Delete a specific stash
git stash push -m "label"Stash with a label
Log & History
7 commandsgit logShow commit history
git log --onelineCompact one-line history
git log --oneline --graph --allBranch graph
git log -pShow patches (diffs) with commits
git blame <file>Show who changed each line
git show <commit>Show a specific commit
git log --author="Name"Filter by author
Undoing & Fixing
7 commandsgit revert <commit>Create a revert commit
git reset --soft HEAD~1Undo last commit, keep staged
git reset --mixed HEAD~1Undo last commit, keep files
git reset --hard HEAD~1Undo last commit and discard changes
git clean -fdRemove untracked files and dirs
git cherry-pick <commit>Apply a commit from another branch
git reflogShow all recent HEAD movements
Tags
4 commandsgit tag v1.0.0Create a lightweight tag
git tag -a v1.0.0 -m "msg"Create an annotated tag
git push origin --tagsPush all tags to remote
git tag -d v1.0.0Delete a local tag
How to use this Git cheat sheet
This reference covers essential Git commands every software developer uses daily. It is structured by developer workflow stages — from initial setup and configuration through branching, committing, remote synchronization, stashing, and undoing accidental mistakes. Use the quick filter chips or live search box to rapidly lookup commands by command syntax or intended description. Click the copy icon next to any command to instantly send it to your clipboard for usage in your terminal. For in-depth CLI documentation on any command, run git help <command> directly in your system terminal.
Built and maintained by Meet Shah · Last updated
What this tool is used for
- Finding a command you use rarely without searching.
- Checking the exact flag for an operation before running it.
- Learning a workflow by seeing its commands grouped.
- Confirming a destructive command's syntax before using it.
- Finding the safer alternative to a command you were about to run.
Frequently Asked Questions
- What is the difference between reset, revert and checkout?
- `reset` moves the branch pointer and can discard commits, `revert` creates a new commit undoing an old one, and `checkout`/`switch` change what you have checked out. Only `revert` is safe on history others have pulled.
- When is force-pushing acceptable?
- On a branch only you use, and then with `--force-with-lease` so it refuses if someone else pushed. On a shared branch it rewrites history under collaborators, which is how work gets lost.
- What does the stash actually do?
- Saves uncommitted changes to a stack and cleans the working tree. It is easy to forget — `git stash list` is worth checking, because stashes survive branch switches and are invisible in normal status output.
- How do I recover a commit I lost?
- `git reflog` records where HEAD has been, including commits no branch points at any more. Almost anything committed can be recovered from there for a couple of weeks before garbage collection.
- Merge or rebase?
- Merge preserves what actually happened; rebase produces a linear history that is easier to read. The practical rule is to rebase your own unpushed work and merge anything already shared.
Common errors and gotchas
- Running a destructive command from a cheat sheet without understanding it, `reset --hard` above all.
- Assuming a command is safe because it is short, where `checkout` on a file discards changes silently.
- Force-pushing to a shared branch, which rewrites history other people have.
- Confusing `reset`, `revert` and `restore`, which do very different things.
- Copying a command with a placeholder left in, which then acts on the wrong ref.