Setup & config ⤢ git configSet your name, email, or editor once for every repo. git config --global user.name "Ada" ⤢ git config --global aliasDefine a shortcut name for a command you type often. git config --global alias.co checkout ⤢ git initCreate a new repository in the current folder. git init ⤢ git cloneCopy a remote repository onto your machine. git clone git@github.com:org/repo.git ⤢ git clone --depthCopy only the newest commits of a single branch. git clone --depth 1 --single-branch git@github.com:org/repo.git Snapshot basics ⤢ git statusShow what is staged, modified, and untracked. git status -sb ⤢ git addStage changes so they enter the next commit. git add src/app.ts ⤢ git add -pReview changes hunk by hunk and stage only the ones you accept. git add -p src/app.ts ⤢ git add -uStage changes to every tracked file without adding new files. git add -u ⤢ git commitRecord the staged changes as a new commit. git commit -m "fix: null check" ⤢ git commit --amendReplace the last commit with a corrected one. git commit --amend -m "fix: handle empty input" ⤢ git diffShow unstaged changes, or staged changes with --staged. git diff --staged ⤢ git diff HEADShow every change since the last commit, staged or not. git diff HEAD Branching & merging ⤢ git branchList, create, or delete branches. git branch feature-x ⤢ git branch -dDelete a merged branch (-D forces it, -a lists every branch). git branch -d feature-x ⤢ git switchSwitch branches (the modern replacement for checkout). git switch feature-x ⤢ git switch -cCreate a new branch and switch to it in one step. git switch -c feature-x ⤢ git checkoutSwitch branches or restore files (the older, broader command). git checkout feature-x ⤢ git mergeBring another branch's changes into the current branch. git merge feature-x ⤢ git merge --abortCancel a conflicted merge and return the branch to its prior state. git merge --abort ⤢ git rebaseReplay the current branch's commits on top of another branch. git rebase main ⤢ git rebase -iSquash, reorder, or edit commits while replaying them. git rebase -i main ⤢ git cherry-pickCopy a single commit from another branch onto this one. git cherry-pick a1b2c3d ⤢ git tagMark a commit with a readable label such as a version. git tag -a v1.2.0 ⤢ git worktreeCheck out another branch in a separate folder at the same time. git worktree add ../repo-feature feature-x Inspecting history ⤢ git logWalk the commit history. git log --oneline --graph --decorate ⤢ git log --allWalk history from every branch, not just the current one. git log --oneline --graph --all ⤢ git showShow the full diff and message of one commit. git show HEAD ⤢ git blameShow who last changed each line of a file. git blame src/app.ts ⤢ git reflogShow every commit HEAD has pointed to, including discarded ones. git reflog ⤢ git bisectSearch history by halves to find the commit that broke something. git bisect start ⤢ git shortlogSummarize commits by author. git shortlog -sn ⤢ git describeName the current commit by counting from the nearest tag. git describe --tags Remote collaboration ⤢ git remoteList or add remote repositories. git remote add origin <url> ⤢ git remote -vList remotes with their fetch and push URLs. git remote -v ⤢ git fetchDownload remote changes without changing your branches. git fetch origin ⤢ git fetch --pruneDownload remote changes and drop refs to deleted remote branches. git fetch --prune ⤢ git pullFetch and merge the current branch's remote changes. git pull --rebase ⤢ git pushSend local commits to the remote. git push -u origin feature-x ⤢ git push --force-with-leaseOverwrite the remote branch, but only if nobody pushed first. git push --force-with-lease origin feature-x Undoing changes ⤢ git restoreDiscard working-tree changes, or unstage them. git restore src/app.ts ⤢ git restore --stagedUnstage a file and keep its edits in the working tree. git restore --staged src/app.ts ⤢ git resetMove the current branch tip and optionally the index. git reset --hard HEAD~1 ⤢ git revertAdd a new commit that undoes an earlier one (safe to share). git revert HEAD ⤢ git cleanDelete untracked files and folders. git clean -fd Stashing ⤢ git stashSave uncommitted work and reset the working tree. git stash -u ⤢ git stash listShow every stash you have saved. git stash list ⤢ git stash applyReapply a stash and keep it stored. git stash apply stash@{1} ⤢ git stash dropDelete a stash you no longer need. git stash drop stash@{1} ⤢ git stash popReapply the most recent stash and drop it. git stash pop Files & submodules ⤢ git mvMove or rename a file and stage the change. git mv old.ts src/new.ts ⤢ git rmDelete a file and stage the deletion. git rm src/old.ts ⤢ git submoduleTrack another repository as a folder inside this one. git submodule add git@github.com:org/lib.git lib ⤢ git sparse-checkoutCheck out only the folders you name from a large repository. git sparse-checkout set src docs ⤢ git archiveExport a commit's tree as a tar or zip file. git archive -o repo.zip HEAD