Git & GitHub — cheat sheet
A one-page lookup for the commands used across this course. Grouped by task; the course pages cover
the why behind each one — this page is just the what.
Inspecting state
| Task |
Command |
| Current branch, staged/unstaged files |
git status |
| Show unstaged changes |
git diff |
| Show staged changes |
git diff --staged |
| Commit history |
git log (git log -p to include diffs) |
| List configured remotes |
git remote -v |
| Recent branch-pointer history (recovery) |
git reflog |
Branches & HEAD
| Task |
Command |
| Create a branch |
git branch <name> |
| Switch to a branch |
git checkout <branch> (or git switch <branch>) |
| Create and switch in one step |
git checkout -b <name> (or git switch -c <name>) |
| Delete a branch |
git branch -d <name> (-D to force) |
| Rescue a detached-HEAD commit |
git branch <name> while it's still checked out |
Committing
| Task |
Command |
| Stage a file |
git add <file> |
| Commit staged changes |
git commit -m "message" |
| Create an annotated tag |
git tag -a v1.0.0 -m "message" |
Merge vs. rebase
| Task |
Command |
| Merge a branch into the current one |
git merge <branch> |
| Rebase the current branch onto another |
git rebase <branch> |
| Continue after resolving a conflict |
git merge: git commit · git rebase: git rebase --continue |
| Abort and back out cleanly |
git merge --abort / git rebase --abort |
| Push after a rebase (own branch only) |
git push --force-with-lease |
Remotes
| Task |
Command |
| Download without integrating |
git fetch origin |
| Download and integrate into the current branch |
git pull origin main |
| Upload a branch's commits |
git push origin <branch> |
Undoing
| Task |
Command |
| Undo a shared/pushed commit (adds a new commit) |
git revert <commit> |
| Move the branch pointer, discard nothing else |
git reset --soft <commit> |
| Move the branch pointer, unstage the diff (default) |
git reset --mixed <commit> |
| Move the branch pointer and discard working files |
git reset --hard <commit> (own, unpushed branch only) |
| Discard uncommitted edits to a file |
git restore <file> |
| Unstage a file without discarding the edit |
git restore --staged <file> |
Stop tracking a file already committed (e.g. after adding it to .gitignore) |
git rm --cached <file> |
How this connects
The git-model page covers what a commit/branch/HEAD/tag actually is, the branching-and-merging page
covers when to reach for merge versus rebase and how to resolve a conflict, the remotes page covers
fetch/pull/push and the GitHub-flow steps around them, and the undoing page covers picking
revert/reset/restore by asking whether the change is already shared.