iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Cheatsheet

The git commands you reach for daily, on one page.

Daily git

EXAMPLE
# Setup once
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 true
git config --global rerere.enabled true

# Start
git init
git clone <url>

# Stage + commit
git status -sb
git add -p                      # interactively pick hunks
git commit -m 'message'
git commit --amend              # only before you push

# Branch + switch
git branch
git switch -c feat/x            # create + switch
git switch main

# Pull + push
git pull --rebase
git push -u origin feat/x

# History
git log --oneline --graph --decorate -20
git log --since='1 week ago'
git log -- src/file.ts          # this path only
git show <sha>
git diff
git diff --staged
git diff main...feat/x

# Undo
git restore file                # discard unstaged
git restore --staged file       # unstage
git reset --soft HEAD^          # undo last commit, keep changes staged
git reset --hard <sha>          # destructive

# Stash
git stash push -m 'msg'
git stash list
git stash pop

# Merge / rebase
git merge feat/x
git rebase main
git rebase -i HEAD~5            # clean up history
git rebase --abort              # bail

# Cherry-pick
git cherry-pick <sha>
git cherry-pick <a>^..<b>       # range

# Tag + release
git tag -a v1.0.0 -m 'release'
git push --tags

# Remote
git remote -v
git remote set-url origin <url>

# Worktree
git worktree add ../hotfix main
git worktree list
git worktree remove ../hotfix

# Submodule
git submodule add <url> vendor/foo
git submodule update --init --recursive

# Bisect
git bisect start; git bisect bad; git bisect good v1.0.0

# Reflog (lifesaver)
git reflog
git reset --hard HEAD@{3}

# Aliases worth setting
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.st 'status -sb'
git config --global alias.lg 'log --oneline --graph --decorate'
git config --global alias.last 'log -1 HEAD'

Why it matters

Bookmark this. After a year of using git daily you will know most of these by heart - until then, keep it open and the muscle memory will follow.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Everyday five:
git status
git add .
git commit -m 'msg'
git pull --rebase
git push
Try it Yourself »

Discussion

Loading…