Git cheat sheet
The git commands you actually use, and the ones you always have to look up: undoing the last commit, cleaning up branches, finding who changed a line, and getting back a commit you thought you lost.
In Snippet Keeper, click Import at the bottom of the sidebar and choose the file. They land in your git app, and ones you already have are skipped. The highlighted parts become fill-in fields: type a value (or pick one), and the finished command is on your clipboard.
Setup
git config --global user.name "name"
git config --global user.email "email"
git config --global init.defaultBranch branch
git clone url
git clone --depth depth url
Everyday
git status -sb
git add -p
git commit -m "message"
git commit --amend --no-edit
git diff
git diff --staged
Branches
git switch -c branch
git switch -
git branch --sort=-committerdate
git branch -m new_name
git branch -d branch
git push origin --delete branch
git branch --merged base | grep -vE '^\*|^\s*base$' | xargs git branch -d
Syncing
git fetch --prune
git pull --rebase
git push -u origin HEAD
git push --force-with-lease
git remote add name url
git fetch upstream && git rebase upstream/branch
Undo
git reset --soft HEAD~count
git restore --staged file
git restore file
git fetch && git reset --hard origin/branch
git revert commit
git reflog
git clean -fdn
Stash
git stash push -u -m "message"
git stash list
git stash pop
git stash apply stash@{index}
git stash push file
History
git log --oneline --graph --decorate --all -n count
git log -p --follow -- file
git log --oneline --grep="text"
git log -S "text" --oneline
git blame -L start,end file
git show commit:path
git log --oneline base..HEAD
Rewriting
git rebase -i base
git rebase -i --autosquash base
git cherry-pick commit
git operation --abort
git bisect start && git bisect bad && git bisect good good_commit
Tags and worktrees
git tag -a tag -m "message"
git push origin --tags
git worktree add ../folder branch
Keep these one keystroke away.
Snippet Keeper is a Mac app for the commands you keep looking up. Press ⌥⇧Space in any app, type a few letters, fill in the blanks, and it’s on your clipboard.