Git Commands Cheat Sheet: Version Control You'll Actually Use
Learn: Git Commands Cheat Sheet: Version Control You'll Actually Use
Welcome to TopperBlog! 👋
I'm a tech content creator passionate about helping developers level up their careers and master cutting-edge technologies.
🎯 What I Write About:
• AI/ML Engineering & LLMs
• Web3 & Blockchain Development
• System Design & Architecture
• Interview Preparation (FAANG)
• Freelancing & Remote Work
• Modern Tech Stacks (Next.js, React, Rust, TypeScript)
• Performance Optimization & Best Practices
💼 Mission: Sharing practical, actionable insights that accelerate your tech career and maximize your earning potential.
📚 15+ In-Depth Guides covering everything from earning $10k/month as a freelancer to cracking FAANG interviews.
🌐 Let's connect and grow together in this amazing tech journey!
#TechBlogger #SoftwareEngineering #CareerGrowth #WebDevelopment #AIEngineering
Git Commands Cheat Sheet: Version Control You'll Actually Use
Beyond add, commit, push basics
Why This Skill Matters
Git isn't just a tool—it's the backbone of modern development. Whether you're working solo or in a team of 100, version control prevents disasters, enables collaboration, and creates an audit trail of every change. Mastering Git transforms you from someone who occasionally loses work to a developer who confidently manages complex projects, recovers from mistakes, and collaborates seamlessly.
The difference between knowing git add and truly understanding Git is the difference between surviving and thriving in professional development.
Getting Started
Installation & Configuration
# Install Git
# macOS: brew install git
# Ubuntu: sudo apt-get install git
# Windows: Download from git-scm.com
# Configure identity (required for commits)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# View all configuration
git config --list
# Set default editor
git config --global core.editor "vim"
Initial Repository Setup
# Create new repository
git init
# Clone existing repository
git clone https://github.com/username/repo.git
git clone https://github.com/username/repo.git custom-folder
# Check repository status
git status
Essential Commands/Shortcuts
Staging & Committing
# Stage specific file
git add filename.js
# Stage all changes
git add .
# Stage with interactive selection (choose hunks)
git add -p
# Commit with message
git commit -m "Fix login validation bug"
# Commit all tracked files (skip staging)
git commit -am "Update documentation"
# Amend last commit (before pushing)
git commit --amend --no-edit
git commit --amend -m "New message"
# View staged changes
git diff --staged
Branching Essentials
# List local branches
git branch
# List all branches (including remote)
git branch -a
# Create new branch
git branch feature/user-auth
# Create and switch to branch
git checkout -b feature/user-auth
git switch -c feature/user-auth # Modern syntax
# Switch to existing branch
git checkout main
git switch main
# Delete branch (local)
git branch -d feature/user-auth
git branch -D feature/user-auth # Force delete
# Delete remote branch
git push origin --delete feature/user-auth
# Rename branch
git branch -m old-name new-name
Pushing & Pulling
# Push to remote
git push origin main
# Push new branch to remote
git push -u origin feature/user-auth
# Pull latest changes
git pull origin main
# Fetch without merging
git fetch origin
# Push all branches
git push --all
# Push with tags
git push --tags
Viewing History
# View commit log
git log
# Compact log view
git log --oneline
# Log with graph visualization
git log --graph --oneline --all
# View commits by author
git log --author="John Doe"
# View commits in date range
git log --since="2024-01-01" --until="2024-12-31"
# View specific file history
git log -- filename.js
# Show specific commit details
git show abc1234
Advanced Techniques
Undoing Changes
# Discard changes in working directory
git checkout -- filename.js
git restore filename.js # Modern syntax
# Unstage file
git reset HEAD filename.js
git restore --staged filename.js
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Revert commit (create new commit that undoes it)
git revert abc1234
# Go back to specific commit
git checkout abc1234
Rebasing & Merging
# Merge branch into current branch
git merge feature/user-auth
# Rebase current branch onto main
git rebase main
# Interactive rebase (squash, reorder commits)
git rebase -i HEAD~3
# Abort rebase if conflicts arise
git rebase --abort
# Continue rebase after resolving conflicts
git rebase --continue
# Merge with squash (combine commits)
git merge --squash feature/user-auth
Stashing Work
# Stash current changes
git stash
# Stash with description
git stash save "WIP: feature implementation"
# List stashes
git stash list
# Apply most recent stash
git stash apply
# Apply specific stash
git stash apply stash@{2}
# Apply and remove stash
git stash pop
# Delete stash
git stash drop stash@{0}
# Delete all stashes
git stash clear
Cherry-Picking & Tags
# Apply specific commit to current branch
git cherry-pick abc1234
# Create annotated tag
git tag -a v1.0.0 -m "Release version 1.0.0"
# Create lightweight tag
git tag v1.0.0
# List tags
git tag
# Push tags to remote
git push origin v1.0.0
git push origin --tags
# Delete tag
git tag -d v1.0.0
git push origin --delete v1.0.0
Practice Drills
Drill 1: Branch Workflow
# Create feature branch from main
git checkout -b feature/search-filter
# Make changes and commit
git add .
git commit -m "Add search filter functionality"
# Push to remote
git push -u origin feature/search-filter
# Switch back to main and pull latest
git checkout main
git pull origin main
Drill 2: Conflict Resolution
# Create conflicting changes on two branches
# Attempt merge
git merge feature/conflicting-branch
# View conflicts
git status
# Edit conflicted files manually
# Stage resolved files
git add resolved-file.js
# Complete merge
git commit -m "Resolve merge conflicts"
Drill 3: History Cleanup
# View last 5 commits
git log --oneline -5
# Squash last 3 commits
git rebase -i HEAD~3
# Mark commits as 'squash' in editor
# Rewrite commit message
# Force push (only on personal branches!)
git push -f origin feature/branch
Integration with Workflow
Daily Development Cycle
# Start day: sync with team
git fetch origin
git pull origin main
# Create feature branch
git checkout -b feature/task-123
# Work and commit regularly
git add .
git commit -m "Implement feature part 1"
git commit -m "Add tests for feature"
# Before pushing: rebase on latest main
git fetch origin
git rebase origin/main
# Push for code review
git push -u origin feature/task-123
# After approval: merge and cleanup
git checkout main
git pull origin main
git merge feature/task-123
git push origin main
git branch -d feature/task-123
Team Collaboration Best Practices
# Meaningful commit messages
git commit -m "Fix: prevent null reference in user validation"
# Keep commits atomic (one logical change per commit)
git add src/validation.js
git commit -m "Add email validation"
git add tests/validation.test.js
git commit -m "Add tests for email validation"
# Pull before pushing
git pull origin main
git push origin feature/branch
# Review your own changes before pushing
git diff origin/main
Pro Tips
Aliases for Speed
# Add to ~/.gitconfig or run:
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'restore --staged'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual 'log --graph --oneline --all'
# Usage
git st # instead of git status
git co main # instead of git checkout main
git visual # instead of git log --graph --oneline --all
Useful Configurations
# Auto-correct typos
git config --global help.autocorrect 1
# Colorize output
git config --global color.ui true
# Set default branch name
git config --global init.defaultBranch main
# Prevent accidental force pushes
git config --global receive.denyForces true
Debugging Commands
# Find which commit introduced a bug
git bisect start
git bisect bad HEAD
git bisect good v1.0.0
# Test each suggested commit until bug found
# Search commit messages
git log --grep="bug fix"
# Find who changed a specific line
git blame filename.js
# Show what changed in a commit
git show abc1234
Summary
Mastering Git separates junior developers from professionals. The commands covered here—from basic staging to advanced rebasing—form a complete toolkit for version control excellence.
Key Takeaways:
- Commit frequently with clear messages
- Branch strategically for features and fixes
- Pull before pushing to avoid conflicts
- Rebase thoughtfully to maintain clean history
- Use aliases to work faster
- Practice regularly with real projects
The best way to internalize Git is through consistent use. Start with the essential commands, gradually incorporate advanced techniques, and build muscle memory through daily practice. Your future self—and your team—will thank you.
Next Steps: Set up your Git aliases today, create a practice repository, and run through the drills. Within a week, these commands will become second nature.
Last updated: 2024 | Git version 2.40+