“I want to…”
Situations mapped to commands, grouped by what you want to do. Each links to the lesson that explains it.
Everyday
See what changed and what is staged
git statusStage a file for the next commit
git add <file>See unstaged / staged changes
git diff · git diff --stagedCommit what is staged
git commit -m "message"Stage all tracked changes and commit
git commit -am "message"Show compact history of everything
git log --oneline --graph --allInspect one commit
git show <commit>Branches
Start a new branch and switch to it
git switch -c <name>Switch to an existing branch
git switch <name>List branches (incl. remote)
git branch -aBring another branch into mine
git merge <branch>Replay my branch on top of main
git rebase mainGive up on a conflicted merge
git merge --abortFinish a conflict
edit files → git add <file> → git commitDelete a merged branch
git branch -d <name>Team & remotes
Download news without changing my work
git fetchUpdate my branch from the remote
git pullPublish a new branch
git push -u origin <branch>My push was rejected
git pull → git pushOverwrite my own remote branch after a rebase
git push --force-with-leaseClean up after my PR was merged
git switch main && git pull && git branch -d <branch>Undo & rescue
Throw away changes to a file
git restore <file>Unstage a file (keep the changes)
git restore --staged <file>Fix the last commit (message or content)
git commit --amend -m "better"Undo last commit, keep changes staged
git reset --soft HEAD~1Undo last commit, keep changes unstaged
git reset HEAD~1Undo last commit and destroy the changes
git reset --hard HEAD~1Undo a commit that is already pushed
git revert <commit>Park unfinished work
git stash → git stash popFind "lost" commits
git reflogGet a lost commit back
git branch rescue <hash>I am in "detached HEAD" and want to keep my work
git switch -c <new-branch>Power tools
Copy one commit onto my branch
git cherry-pick <commit>Work on another branch in a second folder
git worktree add ../hotfix -b hotfixList / remove worktrees
git worktree list · git worktree remove ../hotfixMark a release
git tag v1.0 && git push origin v1.0Squash my last 3 local commits
git reset --soft HEAD~3 && git commit -m "…"