Git Course 0%

Cheat sheets

Six short lists rather than one long one, because the job you are doing decides which you need. Print the ones you use; ignore the rest until you do.

Everyday

Terminal
git status                      # what changed, and on which branch
git diff                        # what changed, in detail
git diff --staged               # what the next commit will contain
git add <file>                  # stage one file
git add -p                      # stage part of a file, interactively
git restore <file>              # discard an edit — permanent
git restore --staged <file>     # unstage, keeping the edit
git commit -m "type: what (#n)" # commit what is staged
git commit --amend              # redo the last commit — before pushing only
git pull                        # get the team's changes
git push                        # publish yours
git log --oneline -10           # the last ten commits

The routine that prevents most mistakes: status, diff, add, diff --staged, commit.

Branches

Terminal
git switch main                 # move to a branch
git switch -c type/12-short-name # create and move to a new one
git switch -                    # back to the previous branch
git branch -a                   # every branch, local and remote
git branch -vv                  # with their upstreams and positions
git push -u origin HEAD         # publish the current branch
git branch -d <branch>          # delete a merged local branch
git branch -D <branch>          # delete it anyway — read the warning first
git fetch --prune               # forget remote branches that are gone
git log --oneline main..HEAD    # what my branch adds
git merge <branch>              # bring another branch in
git rebase main                 # replay mine on top of main — unpushed only

Remotes

Terminal
git clone <url>                 # copy a project to this machine
git remote -v                   # where origin points
git remote set-url origin <url> # point it somewhere else
git fetch                       # download, change nothing
git pull                        # download and integrate
git push                        # publish commits
git push origin <tag>           # publish one tag; a plain push does not
git push --force-with-lease     # publish a rewrite of your own branch

Undo

Terminal
git restore <file>              # discard an edit — permanent
git restore --staged <file>     # unstage
git stash push -m "note"        # park everything
git stash pop                   # bring it back
git commit --amend              # fix the last commit — unpushed
git reset --soft HEAD~1         # undo the commit, keep changes staged
git reset HEAD~1                # undo it, keep changes in the files
git reset --hard HEAD~1         # undo it and discard the changes
git revert <hash>               # undo a pushed commit, safely
git reflog                      # where HEAD has been: the recovery command
git reset --hard ORIG_HEAD      # back to before the last big operation

Conflicts

Terminal
git status                      # which files are unmerged
git diff                        # the conflict, with markers
git add <file>                  # mark one resolved
git commit                      # finish the merge
git merge --abort               # cancel the merge entirely
git rebase --abort              # cancel the rebase entirely
git rebase --continue           # after staging the resolved files
git config --global merge.conflictstyle zdiff3   # show the original too

History

Terminal
git log --oneline --graph -20   # the shape of recent history
git log -- <path>               # commits touching one file
git log -S "<string>" -- <path> # commits that added or removed a string
git log -L 20,40:<file>         # the history of those lines
git show <hash>                 # one commit in full
git blame -w -C -L 20,40 <file> # who last changed those lines
git tag --contains <hash>       # which releases include this commit
git diff v1.2.0..v1.3.0 -- docs # what changed between versions

Setup, once per machine

Terminal
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global pull.rebase false
git config --global merge.conflictstyle zdiff3
git config --global fetch.prune true
git config --global alias.pushf "push --force-with-lease"
git config --list --show-origin        # where every setting came from

The same jobs in an IDE

Job VS Code IntelliJ IDEA
See what changed Source Control view Commit window
Stage + on the file Add to VCS, or the checkbox
Stage part of a file Stage Selected Ranges The gutter marker
Commit Message box, Commit Message box, Commit (⌘K)
Pull and push Sync Changes Update Project, Push
Switch branch The Status Bar branch The VCS widget
Resolve a conflict Resolve in Merge Editor Merge → three panes
History of a file The Timeline view Git → Show History
Recover a lost commit The terminal The terminal

The full table, including the platforms, is Section 17.

The five that matter most

If this page is too much, these five cover most days:

Terminal
git status
git switch -c type/12-short-name
git add <file> && git commit -m "type: what (#12)"
git push -u origin HEAD
git reflog

The last one is there because it is the command that saves you when something has gone wrong (lesson 12.1).