by DannyUpdated 8 sections

Everything here works in Git 2.23 and later. <branch> and <file> are placeholders.

Download the PDF

Set up


_10
git config --global user.name "Your Name"
_10
git config --global user.email [email protected]
_10
git config --global init.defaultBranch main

Start a repository


_10
git init # new repo in this folder
_10
git clone <url> # copy an existing repo
_10
git clone <url> --depth 1 # latest only

Stage and commit


_10
git status # what changed
_10
git add <file> # stage one file
_10
git add -p # stage hunk by hunk
_10
git commit -m "message" # commit what is staged
_10
git commit --amend # fix the last commit

Branches


_10
git switch <branch> # move to a branch
_10
git switch -c <branch> # create and move
_10
git branch # list local branches
_10
git branch -d <branch> # delete a merged branch

Sync with a remote


_10
git fetch # download only
_10
git pull --rebase # fetch, replay yours on top
_10
git push -u origin <branch> # first push
_10
git push # later pushes

Look at history


_10
git log --oneline --graph # compact
_10
git log -p <file> # changes to a file
_10
git diff # unstaged changes
_10
git diff --staged # staged changes
_10
git blame <file> # who wrote each line

Undo things


_10
git restore <file> # discard changes
_10
git restore --staged <file> # unstage
_10
git revert <commit> # commit that undoes one
_10
git reset --soft HEAD~1 # undo commit, keep work

Set work aside


_10
git stash push -m "wip" # shelve changes
_10
git stash list # see what is shelved
_10
git stash apply # bring the latest back
_10
git stash drop # delete the latest