← All Tools

🔀 Git Cheat Sheet

119+ commands • Branching, merging, rebasing • Conventional commits

Setup & Config 9Basic Workflow 12Branching 11Merging & Rebasing 9Stashing 9History & Diff 13Undoing Changes 11Remote Repositories 9Tags 8Advanced 10Conventional Commits 10.gitignore Patterns 8

Setup & Config 9

Set your name
git config --global user.name "Your Name"
Set your email
git config --global user.email "you@example.com"
Set default branch name
git config --global init.defaultBranch main
Set VS Code as editor
git config --global core.editor "code --wait"
Create alias
git config --global alias.co checkout
List all config
git config --list
Initialize new repo
git init
Clone a repo
git clone https://github.com/user/repo.git
Shallow clone (faster)
git clone --depth 1 https://github.com/user/repo.git

Basic Workflow 12

Check working tree status
git status
Stage a specific file
git add file.txt
Stage all changes
git add .
Stage interactively (hunk by hunk)
git add -p
Commit with message
git commit -m "feat: add login"
Stage tracked files + commit
git commit -am "fix: typo"
Edit last commit
git commit --amend
Add to last commit (keep message)
git commit --amend --no-edit
Push to remote
git push origin main
Pull from remote
git pull origin main
Pull with rebase (cleaner history)
git pull --rebase origin main
Fetch all remotes
git fetch --all

Branching 11

List local branches
git branch
List all branches (+ remote)
git branch -a
Create branch
git branch feature-login
Create and switch to branch
git checkout -b feature-login
Create and switch (modern)
git switch -c feature-login
Switch to branch
git checkout main
Switch (modern syntax)
git switch main
Delete branch (safe)
git branch -d feature-login
Force delete branch
git branch -D feature-login
Delete remote branch
git push origin --delete feature-login
Rename branch
git branch -m old-name new-name

Merging & Rebasing 9

Merge branch into current
git merge feature-login
Merge with merge commit
git merge --no-ff feature-login
Squash merge (1 commit)
git merge --squash feature-login
Rebase current onto main
git rebase main
Interactive rebase (last 3 commits)
git rebase -i HEAD~3
Abort rebase
git rebase --abort
Abort merge
git merge --abort
Apply specific commit
git cherry-pick abc1234
Apply commit range
git cherry-pick abc1234..def5678

Stashing 9

Stash changes
git stash
Stash with message
git stash push -m "WIP: login"
List stashes
git stash list
Apply & remove latest stash
git stash pop
Apply latest (keep stash)
git stash apply
Apply specific stash
git stash apply stash@{2}
Delete a stash
git stash drop stash@{0}
Delete all stashes
git stash clear
Stash including untracked files
git stash -u

History & Diff 13

Compact log
git log --oneline
Visual branch graph
git log --oneline --graph --all
Last 5 commits
git log -5
Filter by author
git log --author="Alice"
Filter by date
git log --since="2024-01-01"
History of a file
git log -- file.txt
File history with diffs
git log -p file.txt
Unstaged changes
git diff
Staged changes
git diff --staged
Diff between branches
git diff main..feature
Diff from 2 commits ago
git diff HEAD~2
Show a commit
git show abc1234
Who changed each line
git blame file.txt

Undoing Changes 11

Discard file changes
git checkout -- file.txt
Discard changes (modern)
git restore file.txt
Unstage a file
git restore --staged file.txt
Undo last commit (keep changes)
git reset HEAD~1
Undo last commit (discard changes!)
git reset --hard HEAD~1
Reset to remote state
git reset --hard origin/main
Create undo commit (safe for shared)
git revert abc1234
Remove untracked files & dirs
git clean -fd
Dry run — show what would be removed
git clean -fdn
View all HEAD movements (recovery!)
git reflog
Recover file from reflog
git checkout HEAD@{2} -- file.txt

Remote Repositories 9

List remotes
git remote -v
Add remote
git remote add origin https://github.com/user/repo.git
Rename remote
git remote rename origin upstream
Remove remote
git remote remove upstream
Push & set upstream
git push -u origin main
Safe force push
git push --force-with-lease
Push all tags
git push --tags
Fetch from upstream
git fetch upstream
Change remote URL to SSH
git remote set-url origin git@github.com:user/repo.git

Tags 8

Lightweight tag
git tag v1.0.0
Annotated tag
git tag -a v1.0.0 -m "Release 1.0"
Tag a specific commit
git tag -a v1.0.0 abc1234
List matching tags
git tag -l "v1.*"
Push a tag
git push origin v1.0.0
Push all tags
git push origin --tags
Delete local tag
git tag -d v1.0.0
Delete remote tag
git push origin --delete v1.0.0

Advanced 10

Binary search for bug
git bisect start
git bisect bad
git bisect good abc1234
Work on multiple branches
git worktree add ../feature feature-branch
Add submodule
git submodule add https://github.com/lib/lib.git
Init submodules after clone
git submodule update --init --recursive
Export as zip
git archive --format=zip HEAD > repo.zip
Commit counts by author
git shortlog -sn
Count total commits
git log --all --oneline | wc -l
Stats for last 5 commits
git diff --stat HEAD~5
Create patches for last 3 commits
git format-patch -3
Apply a patch
git apply fix.patch

Conventional Commits 10

New feature
feat: add user authentication
Bug fix
fix: resolve login redirect loop
Documentation
docs: update API documentation
Formatting (no logic change)
style: format with prettier
Code restructure
refactor: extract auth middleware
Performance improvement
perf: cache database queries
Tests
test: add unit tests for auth
Maintenance
chore: update dependencies
CI/CD changes
ci: add GitHub Actions workflow
Breaking change
feat!: drop Node 14 support

.gitignore Patterns 8

Ignore directory
node_modules/
Ignore by extension
*.log
Exception (don't ignore)
!important.log
Environment files
.env
.env.local
.env.*.local
Build outputs
dist/
build/
coverage/
Python artifacts
*.pyc
__pycache__/
*.egg-info/
OS files
.DS_Store
Thumbs.db
Editor files
*.swp
*.swo
.idea/
.vscode/

Made with ♥ by Kas Developer Tools

\xF0\x9F\x92\x99 Tip\xF0\x9F\x93\x9A Get Bundle \x244.99