Git Cheatsheet
Git commands for everyday workflow, branching, remote operations, undo, and advanced usage.
Setup & Init
git initInitialize new git repository in current dir
git clone https://github.com/user/repo.gitClone remote repository
git clone https://github.com/user/repo.git --depth=1Shallow clone (only latest commit, faster)
git config --global user.name "Your Name"Set global username
git config --global user.email "you@example.com"Set global email
git config --global core.editor vimSet default editor
git config --listShow all git config values
Initialize new git repository in current dir
Clone remote repository
Shallow clone (only latest commit, faster)
Set global username
Set global email
Set default editor
Show all git config values
Daily Workflow
git statusShow modified, staged, untracked files
git add file.txtStage specific file
git add .Stage all changes in current directory
git add -pInteractively stage chunks of changes
git commit -m "feat: add login endpoint"Commit staged changes with message
git commit -am "fix: typo in config"Stage tracked files + commit in one step
git diffShow unstaged changes
git diff --stagedShow staged changes (what will be committed)
git log --oneline --graph --allCompact visual branch graph
git log --oneline -10Last 10 commits, one line each
git show HEADShow last commit changes
git show abc1234Show specific commit changes
Show modified, staged, untracked files
Stage specific file
Stage all changes in current directory
Interactively stage chunks of changes
Commit staged changes with message
Stage tracked files + commit in one step
Show unstaged changes
Show staged changes (what will be committed)
Compact visual branch graph
Last 10 commits, one line each
Show last commit changes
Show specific commit changes
Branching
git branchList local branches
git branch -aList local and remote branches
git branch feature/loginCreate new branch (stay on current)
git switch -c feature/loginCreate and switch to new branch
git checkout feature/loginSwitch to existing branch
git merge feature/loginMerge branch into current branch
git merge --no-ff feature/loginMerge with explicit merge commit (no fast-forward)
git rebase mainRebase current branch onto main
git rebase -i HEAD~3Interactive rebase — squash/reword last 3 commits
git branch -d feature/loginDelete merged branch
git branch -D feature/loginForce delete branch (even if unmerged)
git cherry-pick abc1234Apply specific commit onto current branch
List local branches
List local and remote branches
Create new branch (stay on current)
Create and switch to new branch
Switch to existing branch
Merge branch into current branch
Merge with explicit merge commit (no fast-forward)
Rebase current branch onto main
Interactive rebase — squash/reword last 3 commits
Delete merged branch
Force delete branch (even if unmerged)
Apply specific commit onto current branch
Remote
git remote -vList remote connections with URLs
git remote add origin https://github.com/user/repo.gitAdd remote named origin
git fetchDownload remote changes without merging
git fetch --pruneFetch + remove stale remote-tracking branches
git pullFetch + merge remote changes into current branch
git pull --rebaseFetch + rebase instead of merge (cleaner history)
git push origin mainPush local commits to remote
git push -u origin feature/loginPush new branch + set upstream tracking
git push origin --delete feature/loginDelete remote branch
git push --tagsPush all local tags to remote
List remote connections with URLs
Add remote named origin
Download remote changes without merging
Fetch + remove stale remote-tracking branches
Fetch + merge remote changes into current branch
Fetch + rebase instead of merge (cleaner history)
Push local commits to remote
Push new branch + set upstream tracking
Delete remote branch
Push all local tags to remote
Undo & Fix
git restore file.txtDiscard unstaged changes in file
git restore .Discard all unstaged changes
git restore --staged file.txtUnstage file (keep changes)
git revert HEADRevert last commit with new undo commit (safe for shared branches)
git reset --soft HEAD~1Undo last commit — keep changes staged
git reset --mixed HEAD~1Undo last commit — keep changes unstaged
git reset --hard HEAD~1⚠️ Undo last commit and DISCARD all changes
git stashStash current changes temporarily
git stash popApply most recent stash and remove it
git stash listShow all stashed changesets
git stash apply stash@{2}Apply specific stash without removing it
git stash drop stash@{0}Delete specific stash
Discard unstaged changes in file
Discard all unstaged changes
Unstage file (keep changes)
Revert last commit with new undo commit (safe for shared branches)
Undo last commit — keep changes staged
Undo last commit — keep changes unstaged
⚠️ Undo last commit and DISCARD all changes
Stash current changes temporarily
Apply most recent stash and remove it
Show all stashed changesets
Apply specific stash without removing it
Delete specific stash