| git config --global user.name "Name" | Set global username |
| git config --global user.email "x@y" | Set global email |
| git config --global init.defaultBranch main | Default branch = main |
| git config --global core.editor "code --wait" | Set VS Code as editor |
| git config --global color.ui auto | Enable colored output |
| git config --global pull.rebase true | Pull with rebase by default |
| git config --list | View all settings |
| git config user.name | View a specific setting |
| git config --global alias.lg "log ..." | Create shortcut alias |
| git init | Init new repo in current dir |
| git init <name> | Init in new folder |
| git clone <url> | Clone remote repo locally |
| git clone --depth=1 <url> | Shallow clone (latest only) |
| git clone <url> <folder> | Clone into specific folder |
| git remote -v | List configured remotes |
| git remote add origin <url> | Add a remote |
| git remote set-url origin <url> | Change remote URL |
| git remote rename origin upstream | Rename a remote |
| git remote remove <name> | Remove a remote |
| git status | Show working tree status |
| git status -s | Short / compact status |
| git log --oneline | Compact commit history |
| git log --oneline --graph --all | Visual branch graph |
| git log -5 | Last 5 commits |
| git log --author="Name" | Commits by author |
| git log --since="1 week ago" | Commits in last week |
| git log --grep="bugfix" | Search commit messages |
| git show <hash> | Show details of a commit |
| git diff | Unstaged changes |
| git diff --staged | Staged vs last commit |
| git diff main..feature | Diff between branches |
| git diff --name-only | Only filenames changed |
| git blame <file> | Line-by-line authorship |
| git shortlog -sn | Commit count by author |
| Staging | |
| git add <file> | Stage a single file |
| git add . | Stage all changes |
| git add -u | Stage tracked files only |
| git add -p <file> | Stage hunks interactively |
| git restore --staged <file> | Unstage a file |
| git restore <file> | Discard working dir changes |
| Committing | |
| git commit -m "msg" | Commit with message |
| git commit -am "msg" | Stage tracked + commit |
| git commit --amend | Fix last commit (pre-push!) |
| git commit --amend --no-edit | Add files to last commit |
| Stash | |
| git stash | Stash uncommitted work |
| git stash push -m "note" | Stash with description |
| git stash list | View all stashes |
| git stash pop | Restore and remove latest |
| git stash apply stash@{n} | Apply specific stash |
| git stash branch <name> | Create branch from stash |
| git stash drop / clear | Delete stash(es) |
| Managing Branches | |
| git branch | List local branches |
| git branch -a | List all (local + remote) |
| git branch -v | Last commit on each |
| git branch --merged | Show merged branches |
| git checkout -b <name> | Create & switch branch |
| git switch -c <name> | Modern: create & switch |
| git switch <name> | Switch existing branch |
| git branch -m old new | Rename a branch |
| git branch -d <branch> | Delete merged branch |
| git branch -D <branch> | Force delete branch |
| Merging | |
| git merge --no-ff <branch> | Merge with merge commit |
| git merge --abort | Abort in-progress merge |
| git checkout --ours <file> | Keep our version in conflict |
| git checkout --theirs <file> | Keep their version |
| Rebase | |
| git rebase main | Rebase onto main |
| git rebase -i HEAD~3 | Interactive rebase (last 3) |
| git rebase --continue | Continue after conflict fix |
| git rebase --abort | Abort rebase |
| Pushing | |
| git push -u origin main | Push + set tracking |
| git push | Push (tracking already set) |
| git push -u origin <branch> | Push new branch to remote |
| git push --tags | Push all tags |
| git push origin --delete <br> | Delete remote branch |
| Fetching & Pulling | |
| git fetch origin | Download (no merge) |
| git fetch --all | Fetch from all remotes |
| git pull | Fetch + merge |
| git pull --rebase | Fetch + rebase (cleaner) |
| git pull origin main | Pull specific branch |
| Safe Undos | |
| git restore <file> | Discard file changes |
| git restore --staged <file> | Unstage a file |
| git revert <hash> | Safe undo (new commit) |
| git reset (careful!) | |
| git reset --soft HEAD~1 | Undo commit, keep staged |
| git reset HEAD~1 | Undo commit, keep files |
| git reset --hard HEAD~1 | ⚠ Undo + DISCARD all |
| git reset --hard <hash> | ⚠ Reset to specific commit |
| Recovery | |
| git reflog | View ALL HEAD movements |
| git checkout -b recover <hash> | Restore from reflog hash |
| git cherry-pick <hash> | Apply specific commit here |
| git cherry-pick h1^..h2 | Cherry-pick a range |
| Bug Hunting | |
| git bisect start | Start binary search |
| git bisect bad / good | Mark current as bad/good |
| git bisect reset | End bisect session |
| git tag | List all tags |
| git tag -a v1.0.0 -m "msg" | Annotated tag (recommended) |
| git tag v1.0.0 | Lightweight tag |
| git tag -a v0.9 abc123 -m "msg" | Tag a past commit |
| git push origin v1.0.0 | Push single tag |
| git push --tags | Push all tags |
| git tag -d v1.0.0 | Delete local tag |
| git push origin --delete v1.0.0 | Delete remote tag |
| git checkout v1.0.0 | Check out at tag (detached) |
| Hooks | |
| ls .git/hooks/ | List available hooks |
| chmod +x .git/hooks/pre-commit | Enable a hook script |
| Cleaning | |
| git clean -fd | Remove untracked files+dirs |
| git clean -n | Dry run (preview only) |
| Utilities | |
| git archive --format=zip HEAD | Export repo as ZIP |
| git submodule add <url> | Add a submodule |
| git log -p -- <file> | History of a specific file |
| git log --diff-filter=D -- <f> | Find deleted files |
| git grep "pattern" | Search working tree |
| git check-ignore -v <file> | Why is file ignored? |
| Aliases (add to ~/.gitconfig) | |
| alias.st status | git st → git status |
| alias.co checkout | git co → git checkout |
| alias.lg "log --oneline --graph --all" | git lg → visual graph |
git pull --rebase — sync with remote firstgit checkout -b feature/xxx — create branchgit add . → git commit -m "feat: ..."git push -u origin feature/xxx — push branchgit checkout main && git pull — update localgit branch -d feature/xxx — clean up| feat: | New feature for the user |
| fix: | Bug fix for the user |
| docs: | Documentation changes only |
| style: | Formatting, no logic change |
| refactor: | Code restructure, no feature/fix |
| test: | Adding or fixing tests |
| chore: | Build / config / tooling |
| ci: | CI/CD pipeline changes |
| perf: | Performance improvement |
| revert: | Reverts a previous commit |
| Git Flow — Release-based products | |
| main | Production only — always tagged |
| develop | Integration branch for features |
| feature/xxx | One branch per feature from develop |
| release/x.y.z | Release prep — only bug fixes |
| hotfix/xxx | Emergency fix from main |
| GitHub Flow — Continuous deployment | |
| main | Always deployable |
| feature/xxx | Branch → PR → merge = deploy |
| Trunk-Based Dev — High-velocity teams | |
| main (trunk) | All devs commit multiple times/day |
| Feature flags | Hide incomplete features in prod |
| Short branches | Live < 2 days; requires solid CI |
| Frequent Issues | |
| Committed to wrong branch | git reset --soft HEAD~1 → switch branch |
| Detached HEAD | git checkout main OR git checkout -b <name> |
| Push rejected | git pull --rebase, resolve, then push |
| Accidentally deleted branch | git reflog → find hash → git checkout -b name <hash> |
| Committed a secret | Rotate credential immediately + BFG Repo Cleaner |
| .gitignore not working | git rm --cached <file> (file already tracked) |
| Large file blocked | git rm --cached + .gitignore + use Git LFS |
| Wrong commit author | git commit --amend --author (before push) |
| Merge conflict binary | git checkout --ours/--theirs <file> |