Essential Git Commands, Branching & Recovery Cheat Sheet
Quick lookup guide for git staging, interactive rebasing, stashing, undoing mistakes, and team branching workflows.
Table of Contents
Configuration & Initialization
First-time setup and local repository initialization.
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch maingit init # Initialize git in current directory
git clone <repository-url> # Clone remote repository locallyStaging, Commits & History Inspection
Day-to-day workflow commands for tracking file changes and viewing commit logs.
| Command | Action & Description |
|---|---|
| git status | Displays modified, staged, and untracked files in the working tree. |
| git add -p | Interactively stage specific hunks/patches rather than whole files. |
| git commit -m "feat: msg" | Records snapshot of staged changes with a concise commit message. |
| git commit --amend --no-edit | Appends newly staged changes into the most recent commit without altering message. |
| git log --oneline --graph | Visualizes branch topology and recent commits in compact single-line format. |
| git diff --staged | Compares staged changes against the previous commit snapshot. |
Branching, Rebasing & Merging
Branch management and history manipulation.
git switch -c feature/new-tool # Modern: create & switch
git checkout -b feature/new-tool # Classic equivalentgit fetch origin main
git rebase -i origin/maingit cherry-pick <commit-hash>Stashing & Temporary Work
Save uncommitted work without committing so you can switch branches cleanly.
| Command | Usage |
|---|---|
| git stash push -m "WIP: auth" | Shelves modified files with a descriptive label. |
| git stash list | Inspects all saved stash snapshots. |
| git stash pop | Restores the most recent stash and removes it from the stash stack. |
| git stash apply stash@{1} | Applies a specific stash index without deleting it. |
| git stash drop | Discards the topmost stash entry. |
Undoing Changes & Disaster Recovery
Safely recover from mistakes without losing code.
git restore <file-path> # Discard un-staged modifications
git restore --staged <file-path> # Unstage file keeping changes on diskgit reset --soft HEAD~1 # Undo commit, keeps all code staged
git reset --hard HEAD~1 # DANGER: permanently discards commit and changesgit reflog
git reset --hard HEAD@{n}Interactive Client-Side Tools for this Guide
Test and run these patterns directly inside your browser with zero server uploads:
Frequently Asked Questions
When should I use Git Rebase vs Git Merge?
Use git merge when integrating shared public branches (e.g., merging a feature branch into main) to preserve true chronological history. Use git rebase on private local feature branches to keep a clean, linear commit history before opening a pull request.
How can I safely exit a stuck Git rebase or merge?
Run `git rebase --abort` or `git merge --abort`. This safely rolls your repository back to the exact state it was in before initiating the rebase or merge.