Reference
Every command this course teaches. Not all of Git, just about forty of the hundred-and-fifty, chosen because they are the ones you actually reach for.
Naming a commit
HEADwhere you are nowHEAD~2two first-parents backHEAD^2the second parent of a mergemain~1one before main’s tiporigin/mainyour cached copy of the servera1b2c3dany unambiguous hash prefixHEAD@{2}the reflog, two moves agostash@{0}the most recent stashThe three trees
Working directory → staging area → history. Almost all confusion is about which one you are looking at.
git status | what is staged, what is not | |
git add <file> | copy working file → staging area | also marks a conflict resolved |
git add . | stage everything changed | |
git commit -m "<msg>" | staging area → a new commit | |
git commit --amend | replace the last commit | new hash, never after pushing |
git restore <file> | throw away the edit | the one command with no undo |
git restore --staged <file> | unstage, keep the edit | |
git diff | working dir vs staging | |
git diff --staged | staging vs last commit |
Branches & HEAD
A branch is a file containing one hash. HEAD is a pointer to a branch, or, when detached, straight to a commit.
git branch | list branches | |
git branch <name> [<ref>] | create one, without moving | |
git branch -d <name> | delete the pointer | the commits stay |
git switch <branch> | move HEAD to a branch | |
git switch -c <name> | create and move in one step | |
git checkout <hash> | detach HEAD at a commit | |
git tag <name> [<ref>] | a pointer that never moves | |
git log --oneline --graph --all | every branch at once | |
git log A..B | on B but not on A | |
git show <ref> | one commit in detail |
Combining work
Merge records what happened. Rebase rewrites it into a straight line. Neither is right in general.
git merge <branch> | join a branch in | fast-forwards when it can |
git merge --no-ff <branch> | force the merge commit | |
git merge --squash <branch> | stage the result, commit yourself | |
git rebase <upstream> | replay your commits on top | copies, with new hashes |
git rebase --onto <new> <old> <br> | transplant part of a branch | |
git rebase -i <ref> | reorder, squash, fixup, drop | the list runs oldest first |
git cherry-pick <ref> | copy one commit to here |
Conflicts
A conflict is a question, not an error. Git marks both candidates and hands the decision back to you.
git add <file> | mark it resolved | |
git merge --continue | finish the paused merge | |
git rebase --continue | finish the paused rebase | |
git rebase --skip | drop the commit being applied | |
git merge|rebase --abort | full undo, back to the start | |
git checkout --ours <file> | take the branch you are on | flips during a rebase |
git checkout --theirs <file> | take the branch coming in |
Remotes
origin/main is your cached picture of the server, not the server. It only updates when you fetch.
git remote -v | where origin points | |
git fetch | download, change nothing local | always safe |
git pull | fetch, then merge | |
git pull --rebase | fetch, then rebase | keeps history linear |
git push | upload, if it fast-forwards | |
git push --force | overwrite unconditionally | can destroy others’ commits |
git push --force-with-lease | overwrite only what you last saw | use this instead |
Undo
reset rewrites, revert appends. Rule of thumb: rewrite before you push, revert after.
git reset --soft <ref> | move branch only | staging + files untouched |
git reset --mixed <ref> | move branch + reset staging | files untouched |
git reset --hard <ref> | move all three | the only one that loses edits |
git revert <ref> | a new commit that undoes one | safe after pushing |
git reflog | everywhere HEAD has been | how you get anything back |
git reset --hard HEAD@{n} | go back to a reflog entry | |
git stash | park changes, clean the tree | |
git stash pop | put them back |
Anything not on this page will tell you politely that it is not part of the course. That is deliberate, because a simulator that half-implements a command teaches something false.