📅
⏱️8 min read
📊Interactive Tutorial
💻Git

Found this helpful?

Share it with other developers

🔧 DevOps EssentialInteractive Guide

Git Commands Cheat Sheet: Never Get Lost Again

Stop Googling Git commands every 5 minutes. This interactive cheat sheet covers everything from basic commits to advanced workflows - with examples you can try right here.

😤 We've All Been There

"How do I undo that commit?"

"What's the difference between merge and rebase?"

"I accidentally committed to main instead of my feature branch!"

"My Git history is a mess... again."

Sound familiar? You're not alone. Even experienced developers struggle with Git's 150+ commands.

✨ The Solution: Master The Core 20

You don't need to memorize every Git command. Master these 20 commands and you'll handle 95% of real-world scenarios. Try this interactive Git simulator:

🔧

Interactive Git Simulator

Practice Git commands • Safe sandbox environment

Learn Git workflows without fear of breaking anything

🎯 Perfect! Practice Git commands risk-free before using them on real projects.

📚 Essential Commands by Category

1Repository Setup

git init

Initialize new repository

Creates .git folder in current directory

git clone <url>

Copy remote repository

Downloads entire project history

git remote add origin <url>

Connect to remote repository

Usually GitHub, GitLab, or Bitbucket

2Basic Workflow (The Daily Grind)

git status

Check current state

Shows modified, staged, and untracked files

git add .

Stage all changes

Use git add <file> for specific files

git commit -m "message"

Save staged changes

Write clear, descriptive messages

git push

Upload to remote

First time: git push -u origin main

git pull

Download latest changes

Combines fetch + merge

3Branching (Your Safety Net)

git branch

List all branches

* marks current branch

git branch <name>

Create new branch

Doesn't switch to it automatically

git checkout <branch>

Switch to branch

Use git switch <branch> in newer Git

git checkout -b <name>

Create and switch

Combines branch creation + checkout

git branch -d <name>

Delete branch

Use -D to force delete

4Merging (Bringing It Together)

git merge <branch>

Merge branch into current

Creates a merge commit

git rebase <branch>

Reapply commits on top

Cleaner history, but rewrites commits

git merge --no-ff <branch>

Force merge commit

Preserves branch history even for fast-forwards

🚨 Troubleshooting: When Things Go Wrong

❌ "I need to undo my last commit"

# Keep changes, undo commit
git reset --soft HEAD~1
# Discard changes completely
git reset --hard HEAD~1

⚠️ --hard permanently deletes changes!

🔄 "I committed to the wrong branch"

# Move last commit to another branch
git checkout correct-branch
git cherry-pick main
git checkout main
git reset --hard HEAD~1

💡 Cherry-pick copies the commit to target branch

⚔️ "I have merge conflicts"

# See conflicted files
git status
# Edit files to resolve conflicts, then:
git add .
git commit

💡 Look for <<<<<<< markers in conflicted files

↩️ "I want to discard local changes"

# Discard changes to specific file
git checkout -- <file>
# Discard all changes
git checkout -- .

⚠️ This permanently deletes uncommitted changes

🔄 Common Workflows

🌟 Feature Branch Workflow

# Start new feature
git checkout main
git pull
git checkout -b feature-login
# Work on feature
git add .
git commit -m "Add login form"
git push -u origin feature-login
# Merge back
git checkout main
git merge feature-login
git branch -d feature-login

🚨 Hotfix Workflow

# Emergency fix
git checkout main
git checkout -b hotfix-security
# Make urgent fix
git add .
git commit -m "Fix security bug"
git push -u origin hotfix
# Deploy immediately
git checkout main
git merge hotfix-security
git push
git branch -d hotfix-security

💡 Pro Tips

✅ Best Practices

  • Commit early, commit often
  • Write clear commit messages
  • Pull before pushing
  • Use branches for features
  • Review changes before committing

⚠️ Watch Out For

  • ⚠️ Never rebase shared branches
  • ⚠️ Don't commit secrets/passwords
  • ⚠️ Avoid huge commits
  • ⚠️ Don't force push to main
  • ⚠️ Test before committing

🎯 Useful Aliases

Add these to your ~/.gitconfig for faster commands:

[alias]
  s = status
  co = checkout
  br = branch
  cm = commit -m
  unstage = reset HEAD --
  last = log -1 HEAD
  visual = !gitk

📊 Git Flow Visualization

🌳 How Git Branches Actually Work

main:     A---B---C---F---G
                     \\       /
feature:                  D---E

A, B, C: Initial commits on main

D, E: Feature development on branch

F: Merge commit bringing feature back

G: New commit on main after merge

Each commit is a snapshot of your entire project. Branches are just moveable pointers to commits.

🤯

Mind = Blown?

This quick tutorial was just a taste. Want to see the full power of interactive learning? Check out our complete deep-dive into how we built this entire blog with embedded code execution.

CC

Stop Copy-Pasting. Run Code Where You Read It.

The complete story of building the world's first interactive developer blog

📅 8 min read🎯 3 Interactive Labs🔥 2.1k views
📬

Never Miss an Interactive Tutorial

Get new hands-on tutorials delivered weekly. Each one includes live code you can run instantly—no copy-pasting required.

📧Weekly delivery
🚫No spam ever
👥Join 2,500+ developers

Continue Your Learning Journey