Git Cheat Sheet — Complete Command Reference (Daily Use)
Practical Git cheat sheet covering everything from clone/commit to rebase, cherry-pick, bisect, and recovery. Free, bookmark-friendly, with real-world examples.
A practical Git reference — the commands you actually run, organized by what you're trying to do. Not every flag, just the ones that matter daily.
Setup & clone
| Command | What it does |
|---|---|
git config --global user.name "Name" | Set commit author name globally |
git config --global user.email "[email protected]" | Set commit email globally |
git config --global init.defaultBranch main | Default new repos to main instead of master |
git config --global pull.rebase true | Always rebase instead of merge on pull |
git clone <url> | Clone a repo |
git clone --depth 1 <url> | Shallow clone (faster, only latest commit) |
git init | Start a new repo |
Daily workflow
| Command | What it does |
|---|---|
git status | What's staged, what's modified, what's untracked |
git status -s | Short format (one line per file) |
git add <file> | Stage a file |
git add -p | Stage hunks interactively (best for clean commits) |
git add . | Stage all changes in current directory |
git diff | Show unstaged changes |
git diff --staged | Show staged changes |
git diff main | Compare current branch to main |
git commit -m "message" | Commit staged changes |
git commit -am "message" | Stage all tracked files + commit |
git commit --amend | Modify last commit (only if not pushed) |
git commit --amend --no-edit | Add staged changes to last commit, keep message |
Branches
| Command | What it does |
|---|---|
git branch | List local branches |
git branch -a | List local + remote branches |
git branch <name> | Create branch (don't switch) |
git switch <name> | Switch to branch (modern) |
git switch -c <name> | Create + switch (modern) |
git checkout -b <name> | Create + switch (legacy) |
git branch -d <name> | Delete branch (safe — refuses if unmerged) |
git branch -D <name> | Delete branch (force, even if unmerged) |
git branch -m <old> <new> | Rename branch |
git branch --merged main | List branches already merged into main (safe to delete) |
Sync with remote
| Command | What it does |
|---|---|
git fetch | Download remote changes (don't apply) |
git fetch --prune | Fetch + delete remote-tracking refs that no longer exist |
git pull | Fetch + merge (or rebase if configured) |
git pull --rebase | Fetch + rebase (cleaner history) |
git push | Push current branch to remote |
git push -u origin <branch> | First push of new branch (sets upstream) |
git push --force-with-lease | Force push, but safer than --force |
git remote -v | List configured remotes |
git remote add origin <url> | Add a remote |
Inspect history
| Command | What it does |
|---|---|
git log | Full commit history |
git log --oneline | One line per commit |
git log --graph --oneline --all | Visual branch graph (most useful) |
git log -p | Show diffs alongside log |
git log -- <file> | History of a specific file |
git log --author="Alice" | Filter by author |
git log --since="2 weeks ago" | Filter by time |
git log --grep="bug" | Filter by commit message |
git show <commit> | Show one commit's diff + metadata |
git blame <file> | Who last changed each line |
git reflog | Local history of HEAD movements (recovery!) |
Undo & recovery
| Command | What it does |
|---|---|
git restore <file> | Discard unstaged changes (modern) |
git restore --staged <file> | Unstage a file (keep changes) |
git checkout -- <file> | Discard unstaged changes (legacy) |
git reset HEAD <file> | Unstage (legacy) |
git reset --soft HEAD~1 | Undo last commit, keep changes staged |
git reset --mixed HEAD~1 | Undo last commit, keep changes unstaged (default) |
git reset --hard HEAD~1 | Discard last commit + changes (DESTRUCTIVE) |
git revert <commit> | Create a new commit that undoes the given commit (safe for shared branches) |
git clean -n | Preview untracked files that would be deleted |
git clean -fd | Delete untracked files + directories |
Stash
| Command | What it does |
|---|---|
git stash | Save uncommitted changes, return to clean working tree |
git stash -u | Stash including untracked files |
git stash list | List stashes |
git stash pop | Apply most recent stash + remove from list |
git stash apply | Apply most recent stash, keep in list |
git stash drop | Delete most recent stash |
Rebase & cherry-pick
| Command | What it does |
|---|---|
git rebase main | Replay current branch's commits on top of main |
git rebase -i HEAD~5 | Interactive rebase last 5 commits (squash, reorder, edit messages) |
git rebase --continue | After resolving conflicts, continue rebase |
git rebase --abort | Cancel rebase, return to original state |
git cherry-pick <commit> | Apply a single commit from another branch |
git cherry-pick <a>..<b> | Apply a range of commits |
Tags
| Command | What it does |
|---|---|
git tag | List tags |
git tag v1.0.0 | Create a lightweight tag |
git tag -a v1.0.0 -m "Release 1.0" | Create an annotated tag (recommended) |
git push origin v1.0.0 | Push a single tag |
git push --tags | Push all tags |
git tag -d v1.0.0 | Delete tag locally |
Search & debug
| Command | What it does |
|---|---|
git grep "pattern" | Search tracked files (faster than grep) |
git log -S "string" | Find commits that added/removed a string ("pickaxe") |
git log -G "regex" | Find commits whose diff matches regex |
git bisect start | Start binary-search debugging |
git bisect bad / good <commit> | Mark commits during bisect |
git bisect reset | End bisect |
Common scenarios
Oops, committed to wrong branch:
git reset HEAD~1 --soft
git stash
git checkout correct-branch
git stash pop
git commit
Pull failed because of local changes:
git stash
git pull
git stash pop
Recover a deleted branch:
git reflog # find the commit hash
git checkout -b restored <hash>
Squash last 3 commits:
git rebase -i HEAD~3
# In editor: change "pick" to "squash" for commits 2 and 3
Related tools
Comparing two text files line-by-line: diff checker. Verifying a downloaded file's SHA against a published hash: hash generator. Pretty-print API responses while debugging: JSON formatter.
Featured Tools
Try these free tools directly in your browser — no sign-up required.
Hash Generator
Generate cryptographic hashes for any text using MD5, SHA-1, SHA-256, SHA-512, and more. Verify data integrity and create checksums instantly online.
Diff Checker
Compare two texts or code files side by side and highlight differences. Find added, removed, and changed lines instantly with colour-coded diff output.
JSON Formatter
Format, beautify, and validate JSON instantly. Paste raw JSON and get a clean, indented, human-readable output with syntax error detection.