learngit
chapterssandbox

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 now
HEAD~2two first-parents back
HEAD^2the second parent of a merge
main~1one before main’s tip
origin/mainyour cached copy of the server
a1b2c3dany unambiguous hash prefix
HEAD@{2}the reflog, two moves ago
stash@{0}the most recent stash

The three trees

Working directory → staging area → history. Almost all confusion is about which one you are looking at.

git statuswhat is staged, what is not
git add <file>copy working file → staging areaalso marks a conflict resolved
git add .stage everything changed
git commit -m "<msg>"staging area → a new commit
git commit --amendreplace the last commitnew hash, never after pushing
git restore <file>throw away the editthe one command with no undo
git restore --staged <file>unstage, keep the edit
git diffworking dir vs staging
git diff --stagedstaging 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 branchlist branches
git branch <name> [<ref>]create one, without moving
git branch -d <name>delete the pointerthe 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 --allevery branch at once
git log A..Bon 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 infast-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 topcopies, with new hashes
git rebase --onto <new> <old> <br>transplant part of a branch
git rebase -i <ref>reorder, squash, fixup, dropthe 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 --continuefinish the paused merge
git rebase --continuefinish the paused rebase
git rebase --skipdrop the commit being applied
git merge|rebase --abortfull undo, back to the start
git checkout --ours <file>take the branch you are onflips 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 -vwhere origin points
git fetchdownload, change nothing localalways safe
git pullfetch, then merge
git pull --rebasefetch, then rebasekeeps history linear
git pushupload, if it fast-forwards
git push --forceoverwrite unconditionallycan destroy others’ commits
git push --force-with-leaseoverwrite only what you last sawuse this instead

Undo

reset rewrites, revert appends. Rule of thumb: rewrite before you push, revert after.

git reset --soft <ref>move branch onlystaging + files untouched
git reset --mixed <ref>move branch + reset stagingfiles untouched
git reset --hard <ref>move all threethe only one that loses edits
git revert <ref>a new commit that undoes onesafe after pushing
git reflogeverywhere HEAD has beenhow you get anything back
git reset --hard HEAD@{n}go back to a reflog entry
git stashpark changes, clean the tree
git stash popput 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.