☰
Home
/
Cheatsheets
/
Git Cheatsheet
🌿
Git Cheatsheet
Git commands — branching, rebase, stash, reset, bisect
☆
Bookmark
Loading tool…
Git Cheatsheet
55 entries · click to copy
🚀 Setup & Init
git init
Initialize new repository
git clone <url>
Clone remote repository
git clone --depth 1 <url>
Shallow clone (latest commit only)
git config --global user.name 'Name'
Set global username
git config --global user.email 'x@y.com'
Set global email
git remote add origin <url>
Add remote
git remote -v
List remotes
✅ Staging & Committing
git status
Show working tree status
git add .
Stage all changes
git add -p
Interactively stage hunks
git commit -m 'msg'
Commit with message
git commit --amend --no-edit
Add staged changes to last commit
git commit --amend -m 'new msg'
Rewrite last commit message
git restore <file>
Discard working tree changes
git restore --staged <file>
Unstage file
🌿 Branching
git branch
List local branches
git branch -a
List all branches including remote
git checkout -b <branch>
Create and switch to new branch
git switch -c <branch>
Modern: create and switch branch
git branch -d <branch>
Delete merged branch
git branch -D <branch>
Force delete branch
git branch -m old new
Rename branch
🔀 Merging & Rebasing
git merge <branch>
Merge branch into current
git merge --no-ff <branch>
Merge with merge commit always
git merge --squash <branch>
Squash all commits into one staged change
git rebase main
Rebase current branch onto main
git rebase -i HEAD~3
Interactive rebase last 3 commits
git rebase --abort
Abort in-progress rebase
git rebase --continue
Continue after resolving conflicts
git cherry-pick <sha>
Apply specific commit to current branch
☁️ Remote Operations
git fetch --all --prune
Fetch all remotes, remove deleted branches
git pull --rebase
Pull with rebase instead of merge
git push -u origin <branch>
Push and set upstream tracking
git push --force-with-lease
Safe force push (fails if upstream changed)
git push origin --delete <branch>
Delete remote branch
📦 Stash
git stash
Stash working tree changes
git stash push -m 'desc'
Stash with description
git stash list
List stashes
git stash pop
Apply and remove latest stash
git stash apply stash@{2}
Apply specific stash without removing
git stash drop stash@{0}
Delete specific stash
🔍 Log & Diff
git log --oneline --graph --all
Compact visual branch history
git log -p <file>
Show patch history for file
git log --author='Name'
Filter commits by author
git diff
Unstaged changes
git diff --staged
Staged changes
git diff main..feature
Diff between two branches
git blame <file>
Show who changed each line
git bisect start
Start binary search for buggy commit
↩️ Undo & Reset
git revert <sha>
Create new commit undoing a commit (safe)
git reset --soft HEAD~1
Undo last commit, keep staged
git reset --mixed HEAD~1
Undo last commit, keep unstaged
git reset --hard HEAD~1
Undo last commit, discard changes
git clean -fd
Remove untracked files and dirs
git reflog
Show history of HEAD — recovery lifeline