Git Cheatsheet

Everyday Git commands. Each row is 'what you do' → 'how you do it'.

1 credit

Setup

5 items
Configure user
git config --global user.name "Your Name"
Configure email
git config --global user.email "you@example.com"
Default branch
git config --global init.defaultBranch main
Initialize repo
git init
Clone repo
git clone <url>

Staging & committing

6 items
Status
git status
Stage file
git add <path>
Stage all
git add -A
Unstage
git restore --staged <path>
Commit
git commit -m "message"
Amend last
git commit --amend

Branches

5 items
List
git branch -a
Create + switch
git switch -c <name>
Switch
git switch <name>
Delete local
git branch -d <name>
Rename
git branch -m <old> <new>

Remote & sync

5 items
Fetch
git fetch origin
Pull --rebase
git pull --rebase origin main
Push
git push origin <branch>
Push new branch
git push -u origin <branch>
Force-with-lease
git push --force-with-lease

Undo / rescue

6 items
Discard file changes
git restore <path>
Reset soft (keep work)
git reset --soft HEAD~1
Reset hard (destructive)
git reset --hard HEAD~1
Recover via reflog
git reflog
Stash
git stash push -m "msg"
Stash pop
git stash pop

Further reading