📝 Git Cheat Sheet
As a tech guy, git
is a daily life. The git cheat sheet is a MUST you must master.
Getting Started
- Start a new repo:
git init
-
Clone an existing repo:
git clone <url>
Prepare to Commit
-
Add untracked file or unstaged changes:
git add <file>
-
Add all untracked files and unstaged changes:
git add .
-
Choose which parts of a file to stage:
git add -p
-
Move file:
git mv <old> <new>
-
Delete file:
git rm <file>
-
Tell Git to forget about a file (without deleting it):
git rm --cached <file>
-
Unstage one file:
git reset <file>
-
Unstage everything:
git reset
-
Check what you added:
git status
Make Commits
-
Make a commit (open editor):
git commit
-
Make a commit with message:
git commit -m "message"
-
Commit all unstaged changes:
git commit -am "message"
Move Between Branches
-
Switch branches:
git switch <name> # or git checkout <name>
-
Create a branch:
git switch -c <name> # or git checkout -b <name>
-
List branches:
git branch
-
List branches by most recent:
git branch --sort=-committerdate
-
Delete a branch:
git branch -d <name>
-
Force delete a branch:
git branch -D <name>
Diff Staged/Unstaged Changes
-
Diff all staged & unstaged changes:
git diff HEAD
-
Diff just staged changes:
git diff --staged
-
Diff just unstaged changes:
git diff
Diff Commits
-
Show diff between commit & parent:
git show <commit>
-
Diff two commits:
git diff <commit1> <commit2>
-
Diff one file since a commit:
git diff <commit> <file>
-
Show a summary of a diff:
git diff <commit> --stat git show <commit> --stat
Ways to Refer to a Commit
- Branch:
main
- Tag:
v0.1
- Commit ID:
3e887ab
- Remote branch:
origin/main
- Current commit:
HEAD
- Three commits ago:
HEAD^^^
orHEAD~3
Discard Your Changes
-
Delete unstaged changes (one file):
git checkout <file>
-
Delete all staged & unstaged changes (one file):
git checkout HEAD <file>
-
Delete all staged & unstaged changes:
git reset --hard
-
Delete untracked files:
git clean
-
Stash staged & unstaged changes:
git stash
Edit History
-
Undo the most recent commit (keep working dir):
git reset HEAD^
-
Squash the last 5 commits into one:
git rebase -i HEAD~6
-
Undo a failed rebase:
git reflog BRANCHNAME
-
Change a commit message (or add file):
git commit --amend
-
Hard reset to a specific commit:
git reset --hard <commit>
Code Archaeology
-
Look at branch history:
git log main git log --graph main git log --oneline
-
Show commits that modified a file:
git log <file> git log --follow <file>
-
Find commits that added/removed text:
git log -G "banana"
-
Show who last changed each line:
git blame <file>
Combine Diverged Branches
-
Rebase:
git switch banana git rebase main
-
Merge:
git switch main git merge banana
-
Squash merge:
git switch main git merge --squash banana git commit
-
Fast-forward merge:
git switch main git merge banana
-
Cherry-pick a commit:
git cherry-pick <commit>
Restore an Old File
-
Get a version from another commit:
git checkout <commit> <file> # or git restore <file> --source <commit>
Add a Remote
git remote add <name> <url>
Push Your Changes
-
Push main branch:
git push origin main
-
Push current branch:
git push
-
Push a new branch:
git push -u origin <name>
-
Force push:
git push --force-with-lease
-
Push tags:
git push --tags
Pull Changes
-
Fetch changes (no merge):
git fetch origin main
-
Fetch + rebase:
git pull --rebase
-
Fetch + merge:
git pull origin main # or git pull
-
Fetch all branches:
git fetch --all
Configure Git
-
Set a config option:
git config user.name "Your Name"
-
Set option globally:
git config --global ...
-
Add an alias:
git config alias.st status
-
See all options:
man git-config
Important Files
- Local config:
.git/config
- Global config:
~/.gitconfig
-
Ignore list:
.gitignore
-
Previous
From the Speech Recognition Bubble to the AI Boom - Two Decades of Hype, Collapse, and Resurrection -
Next
The Anti-ChatGPT - Why Thomson Reuters AI Slows Down to Speed Up the Future