Ultimate Git Cheat Sheet: Covering CLI & VS Code workflows, authentication, and disaster recovery
Yt key- 245s-09kz-5p6r-hcxa-5gc7
1. Initial Setup
Configure Global Settings (One-Time)
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --global init.defaultBranch main
VS Code Setup
- Install GitLens Extension
- Enable Auto Staging:
Settings > Git: Enable Smart Commit
2. Start a New Project
Local → Remote (CLI)
# Initialize repo
git init
git add .
git commit -m "Initial commit"
# Create remote repo (GitHub/GitLab), then:
git remote add origin <REMOTE_URL>
git push -u origin main
VS Code Workflow
Ctrl+Shift+G→ Initialize Repository- Stage changes (✓ icon) → Commit
- Click
...→ Publish Branch
3. Clone Existing Project
git clone <REMOTE_URL>
cd project
VS Code
Ctrl+Shift+P → “Git: Clone” → Paste URL
4. Daily Workflow
Sync Changes (CLI)
git pull origin main # Pull latest
git add . # Stage files
git commit -m "Message"
git push origin main
VS Code Shortcuts
| Action | Shortcut |
|---|---|
| Stage All | Ctrl+Enter (Commit Box) |
| Commit | Ctrl+Enter |
| Push | Ctrl+P → “push” |
| Pull | Ctrl+P → “pull” |
| View Diffs | Click file in Source Control |
5. Branching Strategy
# Create feature branch
git checkout -b feature/login
# Merge to main
git checkout main
git merge feature/login
# Delete branch
git branch -d feature/login
VS Code
- Bottom-left branch icon → Create New Branch
- After edits: Commit → Push → Create PR via UI
6. Authentication Solutions
HTTPS (Use PAT)
- Generate GitHub PAT
- On first push: Enter username + PAT as password
- Store creds:
git config --global credential.helper store
SSH (Recommended)
ssh-keygen -t ed25519 -C "your@email.com" # Generate key
cat ~/.ssh/id_ed25519.pub # Add to GitHub
git remote set-url origin git@github.com:user/repo.git
VS Code Auth
- Automatically uses system Git credentials
- For PAT:
Ctrl+Shift+P→ “Git: Set Credential Helper” → Select “Store”
7. Recover from Mistakes
| Scenario | CLI Command | VS Code Action |
|---|---|---|
| Undo last commit | git reset HEAD~1 | Commit Box → Undo Last Commit |
| Discard uncommitted | git restore . | File → Discard Changes |
| Recover deleted file | git checkout HEAD -- filename | Source Control → File → Restore |
| Fix broken push | git push --force-with-lease | ... → Push (Force) |
8. Advanced Operations
Stash Unfinished Work
git stash # Save changes
git stash pop # Restore
VS Code: Click Stash Changes in Source Control
Resolve Conflicts
- CLI: Edit conflicted files →
git add .→git commit - VS Code:
- Open conflicted file → Accept “Current” or “Incoming”
- Stage file → Commit
9. Tag Releases
git tag v1.0.0
git push origin v1.0.0
VS Code: ... → Create Tag
10. Disaster Recovery
Local Repo Corrupted
rm -rf .git # Delete local .git
git init # Reinitialize
git remote add origin <URL>
git fetch
git reset --hard origin/main
Recover Lost Commits
git reflog # Find lost commit hash
git checkout <commit-hash> # Restore
VS Code Pro Tips
- Blame Annotations: Right-click line →
GitLens: Blame - Branch Visualization:
Ctrl+Shift+P→ “Git: View History” - Commit Templates:
.gitmessagefile for auto-filled commit messages - Conflict Navigator:
!icon in Source Control view
Industrial Best Practices
.gitignoreTemplate:
# OS files
.DS_Store
Thumbs.db
# Environment
.env
*.key
# Build artifacts
/dist/
/node_modules/
- Branch Protection: Enable in GitHub/GitLab settings
- Pre-commit Hooks: Use Husky for linting/tests
- Semantic Commits:
feat:,fix:,chore:,docs:prefixes
Troubleshooting Cheat Sheet
| Error | Solution |
|---|---|
! [rejected] | git pull --rebase then git push |
Permission denied (publickey) | ssh-add ~/.ssh/id_ed25519 |
Updates were rejected | git pull origin main first |
detected dubious ownership | git config --global safe.directory /path |
Filename too long | git config --global core.longpaths true |
Print this and stick it on your desk!
For GUI lovers: VS Code makes Git 90% clickable. For power users: CLI gives full control.
Remember:git statusis your best friend.
Do include for unrelated histories also,