git cheatsheet
- Posted: January 1, 2026
- Updated: January 1, 2026
This is basically a subset of https://git-scm.com/cheat-sheet.
Getting started
# To start a new repo:
git init
# To clone an existing repo:
git clone <url>
To prepare to commit
# To add untracked file or unstaged changes:
git add <file>
# To add all untracked files and unstaged changes:
git add .
# To choose which parts of a file to stage:
git add -p
# To move a file:
git mv <old> <new>
# To delete a file:
git rm <file>
# To tell git to forget about a file without deleting it:
git rm --cached <file>
# To unstage one file:
git reset <file>
# To unstage everything:
git reset
# To check what you added:
git status
Make commits
# To make a commit (and open text editor to write message):
git commit
# To make a commit:
git commit -m 'message'
# To commit all unstaged changes:
git commit -am 'message'
Move between branches
# To switch to a branch:
git switch <name>
# To create a branch:
git switch -c <name>
# To list branches:
git branch
# To list branches by most recently committed to:
git branch --sort=-committerdate
# To delete a branch:
git branch -d <name>
# To force delete a branch:
git branch -D <name>
Diff staged/unstaged changes
# To diff all staged and unstaged changes:
git diff HEAD
# To diff just staged changes:
git diff --staged
# To diff just unstaged changes:
git diff
Diff commits
Every time you see <commit>, you can use any of these:
- a branch (main)
- a tag (v0.1)
- a commit ID (3e887ab)
- a remote branch (origin/main)
- current commit (HEAD)
- 3 commits ago (HEAD^^^ or HEAD~3)
# To show diff between a commit and its parent:
git show <commit>
# To diff two commits:
git diff <commit> <commit>
# To diff one file since commit:
git diff <commit> <file>
# To show summary of a diff:
git diff <commit> --stat
git show <commit> --stat
Discard changes
# To delete unstaged changed to one file:
git restore <file>
# To delete all staged and unstaged changes to one file:
git restore --staged --worktree <file>
# To delete all staged and unstaged changes:
git reset --hard
# To delete untracked files:
git clean
# To stash all staged and unstaged changes:
git stash
Edit history
# To "undo" most recent commit (keep your working directory the same):
git reset HEAD~1
# To squash the last 5 commits into one:
git rebase -i HEAD~6
# Then change "pick" to "fixup" for any commit you want to combine with the
# previous one.
# To change a commit message (or add a file you forgot):
git commit --ammend
# To undo a failed rebase:
git reflog <branch>
# Then manually find the right commit ID in the reflog, then run:
git reset --hard <commit>
Code archaeology
# To look at a branch history:
git log --graph <branch>
# To show every commit that modified a file:
git log <file>
git log --graph <file>
# To show every commit that modified a file, including before it was renamed:
git log --follow <file>
# To find every commit that added or removed some text:
git log -G banana
# To show who last changed each line of a file:
git blame <file>
Combine diverged branches
# To combine with rebase:
git switch banana
git rebase main
# Before:
# A -> B -> C (main)
# \-> D -> E (banana*)
#
# After:
# A -> B -> C -> D -> E (main and banana*)
# To combine with merge:
git switch main
git merge banana
# Before:
# A -> B -> C (main)
# \-> D -> E (banana*)
#
# After:
# A -> B -> C ------> F (main*)
# \ /
# \-> D -> E (banana)
# To combina with squash merge:
git switch main
git merge --squash banana
git commit
# Before:
# A -> B -> C (main)
# \-> D -> E (banana*)
#
# After:
# A -> B -> C ------> (D|E) (main*)
# \ /
# \-> D -> E (banana)
# To bring a branch up to date with another branch ("fast forward merge"):
git switch main
git merge banana
# Before
# A -> B -> C (main) -> D -> E (banana)
#
# After:
# A -> B -> C -> D -> E (main*|banana)
# To copy one commit onto the current branch:
git cherry-pick <commit>
# Before:
# A -> B -> C (main*)
# \-> D -> E (banana)
#
# After:
# A -> B -> C -> D (main*)
# \
# \-> D -> E (banana)
Restore old file
# To restore an old file from another commit:
git restore <file> --source <commit>
Add remote
# To add remote:
git remote add <name> <url>
Push changes
# To push the main branch to the remote origin:
git push origin main
# To push a branch that you never pushed before:
git push -u origin main
# To push the current branch to its remote "tracking branch":
git push
# To force push:
git push --force-with-lease
# To push tags:
git push --tags
Pull changes
# To fetch changes (but don't change any of your local branches):
git fetch
# To fetch changes and then merge them into your current branch:
git pull
# To fetch changes and then rebase your current branch:
git pull --rebase