Git Cheat Sheet
Searchable Git commands with explanations.
Setup
git config --global user.name "Your Name"Set the name on your commits
git config --global user.email "you@example.com"Set the email on your commits
git config --global init.defaultBranch mainUse main for new repositories
git initStart a repository in the current folder
git clone <url>Copy a remote repository
Everyday
git statusSee changed and staged files
git add <file>Stage a file (git add . for everything)
git commit -m "message"Commit staged changes
git commit -am "message"Stage tracked files and commit in one step
git diffShow unstaged changes
git diff --stagedShow staged changes
git log --oneline --graph --allCompact history of all branches
Branches
git branchList local branches
git switch -c <branch>Create and switch to a new branch
git switch <branch>Switch to an existing branch
git merge <branch>Merge a branch into the current one
git branch -d <branch>Delete a merged branch
git branch -m <new-name>Rename the current branch
Remote
git remote add origin <url>Connect a remote repository
git push -u origin <branch>Push a branch and track it
git pullFetch and merge from the tracked branch
git pull --rebaseFetch and replay your commits on top
git fetch --pruneUpdate remote branches and remove deleted ones
git remote -vShow remote URLs
Undo
git restore <file>Discard unstaged changes to a file
git restore --staged <file>Unstage a file, keep the changes
git commit --amendEdit the last commit (before pushing)
git reset --soft HEAD~1Undo the last commit, keep changes staged
git revert <commit>Create a new commit that undoes one (safe after pushing)
git reflogFind lost commits and branch positions
Stash
git stashSave uncommitted changes for later
git stash -uStash including untracked files
git stash listList stashes
git stash popRe-apply the latest stash and drop it
Rebase & history
git rebase mainReplay your branch on top of main
git rebase -i HEAD~3Edit, squash or reorder the last 3 commits
git cherry-pick <commit>Copy one commit onto the current branch
git blame <file>See who last changed each line
git show <commit>Show a commit's changes
Tags
git tag v1.0.0Tag the current commit
git tag -a v1.0.0 -m "Release"Create an annotated tag
git push origin --tagsPush all tags
Clean up
git rm --cached <file>Stop tracking a file but keep it on disk
git clean -nPreview untracked files that would be deleted
git clean -fdDelete untracked files and folders (careful!)
How to use
- 1Search for what you want to do, like undo or rename.
- 2Read the one-line explanation.
- 3Copy the command.
- 4Replace placeholders like <branch> before running it.
Frequently asked questions
How do I undo the last commit?
git reset --soft HEAD~1 keeps your changes staged. If you've already pushed, use git revert <commit> instead.
What's the difference between merge and rebase?
Merge joins histories with a merge commit; rebase replays your commits on top for a straight history. Don't rebase shared branches.
How do I rename a branch?
git branch -m new-name renames the current branch.
How do I save work without committing?
git stash saves it; git stash pop brings it back.
Is git switch the same as git checkout?
git switch is the newer, clearer command for changing branches; checkout still works.
You may also need
URL Parser
Split URLs, edit query params, build UTM links.
JSON Formatter
Format, validate and minify JSON.
Password Generator
Generate strong, random passwords.
Base64 Encoder / Decoder
Encode text to Base64 or decode it back.
URL Encoder / Decoder
Encode or decode URLs and see every query parameter.
JWT Decoder
Decode JSON Web Tokens and check expiry times.