🌿 Version Control Cheatsheet
Git CLI Quick Reference
Essential daily commands for repository setup, staging, committing, branching, remote synchronization, stashing, rebasing, and emergency undo operations.
⚙️ Config & Setup
📝 Staging & Commits
🌿 Branching & Merging
🚀 Remotes & Sync
📦 Stash & Worktrees
🔍 Inspect & History
⏪ Undo & Reset
⚙️ Configuration & Initialization
| Command | Description |
|---|---|
| git init | Initialize a new local Git repository in the current working directory. |
| git clone <repo-url> | Clone a remote repository into a new local directory. |
| git config --global user.name "Your Name" | Set the author name to be attributed to future commits across all repos. |
| git config --global user.email "you@example.com" | Set the author email address associated with your Git commits. |
| git config --list --show-origin | Show all active Git configuration keys along with the file where each was defined. |
📝 Staging & Committing Changes
| Command | Description |
|---|---|
| git status -s | Show a concise status report of modified, staged, and untracked files. |
| git add . | Stage all new, modified, and deleted files for the next commit. |
| git add -p | Interactively choose individual hunks/patches to stage, keeping other lines unstaged. |
| git commit -m "feat: descriptive message" | Record staged changes to repository history with a commit message. |
| git commit --amend --no-edit | Add recently staged files directly into the previous commit without altering its message. |
🌿 Branching & Merging
| Command | Description |
|---|---|
| git switch -c <branch-name> | Create a new branch and immediately switch into it (modern replacement for checkout -b). |
| git switch - | Quickly toggle back to the previously active branch. |
| git branch -a | List all local and remote-tracking branches. |
| git merge <branch> --no-ff | Merge target branch into current branch, explicitly preserving merge history with a merge commit. |
| git rebase main | Re-apply current branch commits on top of the latest tip of main for a clean linear history. |
| git branch -d <branch> | Safely delete a local branch that has already been merged (use -D to force delete). |
🚀 Remotes & Collaboration
| Command | Description |
|---|---|
| git remote -v | List all configured remote repository names and fetch/push URLs. |
| git fetch origin --prune | Download objects from remote and prune deleted remote branches from your local tracking list. |
| git pull --rebase origin main | Fetch upstream changes and replay your unpushed commits on top, avoiding ugly merge bubbles. |
| git push -u origin <branch> | Push local branch to remote and set upstream tracking link for simple future git push. |
| git push --force-with-lease | Safely force-push after rebasing; prevents overwriting teammates' work if remote has new commits. |
📦 Stash & Worktrees
| Command | Description |
|---|---|
| git stash push -u -m "WIP: feature x" | Shelve modified & untracked files (-u) with a custom note to clean your working tree. |
| git stash pop | Restore the most recently stashed changes and remove them from the stash stack. |
| git stash list | Display all currently shelved stashes with their indexes (e.g. stash@{0}). |
| git worktree add ../hotfix-dir hotfix-branch | Check out multiple branches at once in separate linked directories without switching contexts. |
🔍 Inspecting & History
| Command | Description |
|---|---|
| git log --oneline --graph --decorate --all | Render a beautiful compact ASCII graph of all branches and commit history. |
| git diff | Show unstaged line-by-line code changes in your working directory. |
| git diff --staged | Show exact diff of changes currently in the staging index ready for commit. |
| git blame -L 10,25 <file> | Inspect author, commit hash, and timestamp for each line in a given line range. |
⏪ Undoing Changes & Disaster Recovery
| Command | Description |
|---|---|
| git restore <file> | Discard uncommitted modifications in a working copy file, restoring it to HEAD. |
| git restore --staged <file> | Unstage a file without losing its modifications in your working directory. |
| git revert <commit-hash> | Safely invert and negate the changes of a past commit by creating a new forward commit. |
| git reset --soft HEAD~1 | Undo the latest commit but keep all file changes staged ready for edits. |
| git reset --hard HEAD~1 | Nuclear reset: undo latest commit and discard all modifications completely. |
| git reflog | Life-saver log recording every single HEAD movement to recover "lost" commits or resets. |