← All sheets

⎇ Git Cheat Sheet

The commands that cover 95% of daily Git usage, from setup to advanced history rewriting.

Setup & config
git config --global user.name "Name" Set your name for all repos on this machine
git config --global user.email "e@mail" Set your email
git config --global core.editor vim Set your default commit editor
git config --list Show all active config values
git init Initialise a new local repository
git clone <url> Clone a remote repository locally
git clone --depth 1 <url> Shallow clone (latest commit only, faster)
Staging & committing
git status Show working tree status
git diff Show unstaged changes
git diff --staged Show staged (indexed) changes
git add <file> Stage a specific file
git add -p Interactively stage hunks
git restore --staged <file> Unstage a file (keeps working-tree changes)
git restore <file> Discard unstaged changes in a file
git commit -m "message" Commit with an inline message
git commit --amend --no-edit Add staged changes to the last commit without changing the message
git commit --amend -m "msg" Rewrite the last commit message
Tip: Never amend commits that have already been pushed to a shared branch — it rewrites history and forces others to rebase.
Branches
git branch List local branches
git branch -a List local and remote branches
git switch -c <branch> Create and switch to a new branch
git switch <branch> Switch to an existing branch
git branch -d <branch> Delete a fully merged branch
git branch -D <branch> Force-delete a branch (even if unmerged)
git branch -m old new Rename a branch
Merging & rebasing
git merge <branch> Merge branch into current branch (creates a merge commit)
git merge --no-ff <branch> Force a merge commit even if fast-forward is possible
git merge --squash <branch> Squash all branch commits into the index (then commit manually)
git rebase <branch> Rebase current branch onto another
git rebase -i HEAD~N Interactive rebase — squash, edit, reorder last N commits
git rebase --abort Cancel an in-progress rebase
git rebase --continue Continue after resolving rebase conflicts
git cherry-pick <hash> Apply a specific commit onto the current branch
Remotes
git remote -v List remotes with their URLs
git remote add origin <url> Add a remote named "origin"
git fetch --prune Download remote changes and remove stale remote-tracking branches
git pull --rebase Pull and replay your local commits on top
git push -u origin <branch> Push and set the upstream tracking branch
git push --force-with-lease Force-push safely — fails if the remote has new commits
git push origin --delete <branch> Delete a remote branch
Stash
git stash Stash all tracked changes
git stash -u Stash including untracked files
git stash list Show all stashes
git stash pop Apply the latest stash and remove it from the list
git stash apply stash@{2} Apply a specific stash without removing it
git stash drop stash@{0} Delete a specific stash
Log & search
git log --oneline --graph --decorate --all Compact visual graph of all branches
git log -p <file> Show patch history for a file
git log --author="name" Filter commits by author
git log --since="2 weeks ago" Filter commits by date
git log -S "search string" Find commits that added or removed a string (pickaxe)
git blame <file> Show who last changed each line of a file
git show <hash> Show the diff and metadata of a commit
git bisect start Start a binary search for the commit that introduced a bug
Undoing things
git revert <hash> Create a new commit that undoes a past commit (safe)
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 and discard all changes (destructive)
git clean -fd Remove untracked files and directories
git reflog Show the full history of HEAD — find "lost" commits here
Tip: git reflog is your safety net. Even after a --hard reset, you can recover commits via their hash for ~30 days.