Nation Now Samachar

Ultimate Git Cheat Sheet – Industrial Projectsgit cheat sheet

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

  1. Install GitLens Extension
  2. 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

  1. Ctrl+Shift+GInitialize Repository
  2. Stage changes (✓ icon) → Commit
  3. 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

ActionShortcut
Stage AllCtrl+Enter (Commit Box)
CommitCtrl+Enter
PushCtrl+P → “push”
PullCtrl+P → “pull”
View DiffsClick 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

  1. Bottom-left branch icon → Create New Branch
  2. After edits: CommitPush → Create PR via UI

6. Authentication Solutions

HTTPS (Use PAT)

  1. Generate GitHub PAT
  2. On first push: Enter username + PAT as password
  3. 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

ScenarioCLI CommandVS Code Action
Undo last commitgit reset HEAD~1Commit Box → Undo Last Commit
Discard uncommittedgit restore .File → Discard Changes
Recover deleted filegit checkout HEAD -- filenameSource Control → File → Restore
Fix broken pushgit 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

  1. CLI: Edit conflicted files → git add .git commit
  2. 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

  1. Blame Annotations: Right-click line → GitLens: Blame
  2. Branch Visualization: Ctrl+Shift+P → “Git: View History”
  3. Commit Templates: .gitmessage file for auto-filled commit messages
  4. Conflict Navigator: ! icon in Source Control view

Industrial Best Practices

  1. .gitignore Template:
   # OS files
   .DS_Store
   Thumbs.db

   # Environment
   .env
   *.key

   # Build artifacts
   /dist/
   /node_modules/
  1. Branch Protection: Enable in GitHub/GitLab settings
  2. Pre-commit Hooks: Use Husky for linting/tests
  3. Semantic Commits: feat:, fix:, chore:, docs: prefixes

Troubleshooting Cheat Sheet

ErrorSolution
! [rejected]git pull --rebase then git push
Permission denied (publickey)ssh-add ~/.ssh/id_ed25519
Updates were rejectedgit pull origin main first
detected dubious ownershipgit config --global safe.directory /path
Filename too longgit 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 status is your best friend.

Do include for unrelated histories also,