Git & GitHub Command Cheat Sheet: From Beginner Commands to Resolving Merge Conflicts
A complete developer reference guide covering daily Git CLI commands, branching workflows, interactive rebasing, stashing, and conflict resolution.
Summary: Master Git CLI operations. Comprehensive reference guide covering setup, staging, branching, interactive rebasing, stashing, recovering lost commits via reflog, and resolving merge conflicts.
1. Daily Setup & Basic Staging Commands
Configure identity and repository setup: * **Identity:** git config --global user.name "Your Name" * **Email:** git config --global user.email "you@example.com" * **Init:** git init * **Remote:** git remote add origin https://github.com/username/repository.git * **Stage & Commit:** git add . && git commit -m "feat: add user authentication API"
2. Branching & GitFlow Strategy
* **Create Feature Branch:** `git checkout -b feature/payment-gateway` * **List All Branches:** `git branch -a` * **Push Branch Remote:** `git push -u origin feature/payment-gateway`
3. Advanced History Rewriting: Interactive Rebase
Clean up messy commit histories before creating a pull request: `git rebase -i HEAD~4` In the interactive editor, choose actions: * **pick:** Keep the commit. * **squash:** Combine commit into previous commit. * **reword:** Change commit message. * **drop:** Remove commit.
4. Step-by-Step Guide to Resolving Merge Conflicts
When a merge conflict occurs during git rebase:
1. Open conflicting files in VS Code.
2. Locate merge conflict markers (<<<<<<< HEAD, =======, >>>>>>>).
3. Select desired code changes and save files.
4. Stage resolved files: `git add
5. Emergency Recovery: Git Reflog & Stash Masterclass
### Recovering Lost Commits with Reflog If you accidentally hard reset a branch (git reset --hard), Git retains lost commits in its reference log: 1. View reference log history: `git reflog` 2. Restore repository state: `git reset --hard HEAD@{3}`