Git is a distributed version control system. This cheat sheet covers the commands used most often — from first-time setup to interactive rebasing — with copy-paste ready examples.
1. Setup & Configuration
Command
What it does
git config --global user.name "Your Name"
Set your display name for commits
git config --global user.email "you@example.com"
Set your email for commits
git config --global core.editor "code --wait"
Set VS Code as default editor
git config --global init.defaultBranch main
Use main as default branch name
git config list
View all current config values (older spelling: --list)
git init
Initialize a new repo in current directory
git clone <url>
Clone a remote repo locally
git clone --branch <name> --single-branch <url>
Clone only the history of one branch
2. Daily Workflow
The everyday loop is four commands: git pull → edit → git add . → git commit -m "msg" → git push.
Command
What it does
git status
Show working tree status
git add .
Stage everything under the current directory
git add -p
Stage changes interactively (hunk by hunk)
git commit -m "message"
Commit staged changes
git commit -am "message"
Stage tracked files and commit in one step
git commit --amend
Modify the last commit
git push -u origin <branch>
Push and set upstream tracking
git pull
Fetch and integrate remote changes (fast-forward only by default)
git fetch
Download remote changes without merging
Tip: Use git commit --amend --no-edit to add forgotten files to the last commit without changing the message.
3. Branching & Merging
Command
What it does
git branch
List local branches
git branch -a
List all branches including remote
git checkout -b <name>
Create and switch to a new branch
git switch -c <name>
Modern syntax: create and switch
git branch -d <name>
Delete a merged branch (safe)
git branch -D <name>
Force-delete a branch
git merge <branch>
Merge branch into current branch
git merge --no-ff <branch>
Merge with a merge commit
git merge --squash <branch>
Stage the branch as one combined change, without a commit or merge record
git cherry-pick <hash>
Apply a specific commit to current branch
4. Remote Repositories
Command
What it does
git remote -v
List remote connections with URLs
git remote add origin <url>
Add a remote named "origin"
git remote set-url origin <url>
Change remote URL
git push origin --delete <branch>
Delete a remote branch
git fetch --prune
Remove stale remote-tracking branches
git push --force-with-lease
Safer force push
Avoidgit push --force on shared branches. Use --force-with-lease instead.
5. Undoing Mistakes
Command
What it does
git restore <file>
Discard unstaged changes to a file
git restore --staged <file>
Unstage a file
git reset --soft HEAD~1
Undo last commit, keep changes staged
git reset HEAD~1
Undo last commit, keep changes unstaged
git reset --hard HEAD~1
Undo last commit and discard all changes
git revert <commit>
Create a new commit undoing a previous one (safe for shared branches)
git clean -fd
Remove untracked files and directories (ignored files stay)
git reflog
View recent HEAD movements — your recovery safety net
Recovery: Lost a commit to git reset --hard? Run git reflog to find its hash, then git reset --hard <hash> to go back to it. Uncommitted changes that the reset discarded are not in the reflog.
6. Stashing
Command
What it does
git stash
Stash current changes
git stash -u
Stash including untracked files
git stash push -m "description"
Stash with a description
git stash list
Show all stashes
git stash pop
Restore most recent stash and delete it
git stash apply stash@{2}
Restore a specific stash (keeps it)
git stash drop stash@{1}
Delete a specific stash
git stash clear
Delete all stashes
7. Rebasing
Rebase rewrites history for a linear timeline. Use on local branches only.
Command
What it does
git rebase main
Replay current branch commits on top of main
git rebase -i HEAD~5
Interactive rebase: edit, squash, reorder, drop last 5 commits
git rebase --continue
Continue after resolving a conflict
git rebase --abort
Cancel the rebase
8. Tags & Releases
Command
What it does
git tag -a v1.0.0 -m "Release 1.0"
Create an annotated tag
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
9. History & Diffs
Command
What it does
git log --oneline --graph --all
Visual branch graph in terminal
git log -p <file>
Show all changes to a specific file
git diff --staged
Show staged changes (what will be committed)
git diff main..feature
Compare two branches
git blame <file>
Show who changed each line and when
10. Advanced Tips
Command
What it does
git log -S"function_name"
Find commits that added/removed a specific string
git bisect start
Binary search to find the commit that introduced a bug
Git's commands make sense once three places are clear: the working tree (files on disk), the index or staging area (what the next commit will contain) and the repository (commits). add moves changes from the tree to the index, commit from the index to the repository, and the undo commands in the sheet are named by which of those they touch — restore for the tree, restore --staged for the index, reset and revert for commits. Reading the sheet with that map removes most of the guesswork.
Safe versus destructive
Commands that only add history — commit, merge, revert, tag, push without force — are safe to try. The ones that rewrite or discard — reset --hard, rebase, push --force, checkout -- file, clean — are the ones to run with a clear head; the sheet carries a warning for the force push and a note on rebase. Commits dropped by a reset or a rebase can usually be found again through reflog, whose entries for unreachable commits are kept 30 days by default; it closes the Undoing Mistakes table here and is the first place to look after a mistake.
Where this goes next
The git page on this site has a longer list of commands with FAQs and a source reference, and the FAQs there answer how to undo the last commit, what a stash stores and how to restore it, and when to rebase instead of merge. Aliases in ~/.gitconfig are the usual next step once the sheet is familiar.