A complete, hands-on guide to version control that actually makes sense
Before We Begin: Why Git Matters
I'll never forget my first week as a junior developer. I'd been working on a feature for three days when my computer crashed. The file corrupted. Three days of work, gone forever. I literally cried at my desk.
My senior dev walked over, looked at my screen, and said something I'll never forget: "You didn't use Git?"
That night, I learned Git. I've never lost work since.
Git is a time machine for your code. It lets you:
- Travel back to any previous version of your project
- Work on multiple features simultaneously without them interfering
- Collaborate with others without accidentally overwriting their work
- Experiment freely, knowing you can always undo
This tutorial will take you from "Git? Isn't that a type of fruit?" to confidently using Git in your daily workflow.
What you'll learn:
- Core concepts explained simply
- Step-by-step commands with real examples
- Daily workflows you'll actually use
- How to fix mistakes (because you will make them)
Prerequisites:
- A computer (Mac, Windows, or Linux)
- Ability to open a terminal/command prompt
- A text editor
Time to complete: About 1-2 hours
Let's start your Git journey.
Part 1: What Is Version Control? (The Mental Model)
The Problem Git Solves
Imagine you're writing an essay. You might do this:
essay_final.docx
essay_final2.docx
essay_final_really_final.docx
essay_final_OMG_this_time_for_real.docx
We've all been there. This is manual version control, and it's terrible.
Git solves this by tracking every change automatically. Think of it as:
A snapshot machine — Every time you save, Git takes a picture of your entire project
A time machine — You can go back to any snapshot
A parallel universe generator — Create alternate timelines to try ideas safely
The Three States of Git
Everything in Git exists in one of three states:
Working Directory → Staging Area → Repository
(your files) (ready to save) (saved forever)
Think of it like preparing a photo album:
Working Directory — You take photos (make changes to files)
Staging Area — You select which photos to put in the album (choose what to save)
Repository — You paste them in the album permanently (commit)
Let's see this in action.
Part 2: Installing and Configuring Git
Step 1: Install Git
Mac:
# Using Homebrew (recommended)
brew install git
# Or download from git-scm.com
Windows:
Download from and sign in
Step 2: Connect Your Local Repo to GitHub
GitHub will show you commands. Run:
git remote add origin https://github.com/YOUR-USERNAME/my-first-git-project.git
This tells Git: "There's a remote repository called 'origin' at this URL."
Step 3: Push Your Code to GitHub
git push -u origin main
The -u sets up tracking so future pushes can just use git push.
Step 4: Check GitHub
Refresh your GitHub page. Your code is now online!
Step 5: Pull Changes from GitHub
If someone else pushes changes, or you work on another computer:
git pull origin main
This downloads and merges changes from GitHub.
Part 11: The Daily Developer Workflow
Here's what your actual day-to-day workflow looks like:
Morning: Start Fresh
git checkout main
git pull origin main
Work on a New Feature
git checkout -b feature/awesome-thing
# Write code...
git add .
git commit -m "Add awesome feature part 1"
# Write more code...
git add .
git commit -m "Finish awesome feature"
Update Your Branch with Latest Changes
git checkout main
git pull origin main
git checkout feature/awesome-thing
git merge main
# Fix any conflicts
Push and Create Pull Request
git push origin feature/awesome-thing
Then on GitHub, create a Pull Request for your feature.
Part 12: Handy Shortcuts and Aliases
Git has many shortcuts that save time:
Staging Shortcuts
# Stage all changes (including new files)
git add -A
# Stage all changes in current directory
git add .
# Interactive staging (choose what to stage)
git add -p
Commit Shortcuts
# Amend last commit (if you forgot something small)
git commit --amend
# Commit with a shorter message flag
git commit -m "Message"
Log Shortcuts
# One-line log
git log --oneline
# Graph view
git log --graph --oneline --all
# Last 3 commits
git log -3
# Search commits by message
git log --grep="bug fix"
Create Aliases (Even Shorter!)
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.hist "log --oneline --graph --all"
Now you can use:
git st # Instead of git status
git co main # Instead of git checkout main
git hist # Pretty history view
Part 13: The .gitignore File – What Not to Save
Some files should NEVER be in Git:
- Passwords and secrets
- Environment variables
- Build outputs (like
node_modules) - Temporary files
- IDE settings
Step 1: Create .gitignore
touch .gitignore
Step 2: Add Patterns to Ignore
Edit .gitignore:
# Dependencies
node_modules/
vendor/
# Build outputs
dist/
build/
*.exe
*.dll
# Environment files
.env
.env.local
# IDE files
.vscode/
.idea/
*.swp
# OS files
.DS_Store
Thumbs.db
# Logs
*.log
npm-debug.log*
Step 3: Add and Commit
git add .gitignore
git commit -m "Add .gitignore"
Git will now ignore all those files automatically.
Part 14: Fixing Common Mistakes
Mistake 1: Committed to the Wrong Branch
# You committed to main but meant to commit to feature
git checkout -b feature-branch # Creates branch at current commit
git checkout main
git reset --hard HEAD~1 # Remove commit from main
Mistake 2: Need to Undo a Merge
# If you haven't pushed yet
git reset --hard ORIG_HEAD
# ORIG_HEAD is Git's backup of where you were before the merge
Mistake 3: Committed with Wrong Message
git commit --amend -m "Correct message"
Mistake 4: Accidentally Deleted a File
# Restore a deleted file from the last commit
git restore deleted-file.txt
# Or from a specific commit
git restore --source=abc1234 deleted-file.txt
Mistake 5: Need to See What Changed in a Commit
git show abc1234
Part 15: A Complete Real-World Example
Let's walk through a realistic feature development cycle:
# Start fresh
git checkout main
git pull origin main
# Create feature branch
git checkout -b feature/user-authentication
# Create new files
mkdir -p src/components
touch src/components/LoginForm.js
touch src/components/RegisterForm.js
# Stage and commit
git add src/components/LoginForm.js
git commit -m "Add login form component"
git add src/components/RegisterForm.js
git commit -m "Add registration form component"
# Oops, forgot to add styles
touch src/components/LoginForm.css
git add src/components/LoginForm.css
git commit --amend --no-edit # Add to previous commit
# Meanwhile, main has been updated. Get those changes.
git checkout main
git pull origin main
git checkout feature/user-authentication
git merge main
# Fix any conflicts, then continue
# Add authentication logic
touch src/utils/auth.js
git add src/utils/auth.js
git commit -m "Add authentication utility functions"
# Push and create pull request
git push origin feature/user-authentication
Part 16: Git Cheat Sheet (Keep This Handy)
Getting Started
git init # Create new repo
git clone <url> # Copy existing repo
git config --global user.name "Name" # Set identity
Daily Work
git status # What's happening?
git add <file> # Stage file
git add . # Stage all
git commit -m "message" # Commit staged
git commit -a -m "message" # Stage+commit tracked files
git pull # Get latest changes
git push # Send your changes
Branches
git branch # List branches
git branch <name> # Create branch
git checkout <name> # Switch branch
git checkout -b <name> # Create and switch
git merge <name> # Merge branch into current
git branch -d <name> # Delete branch
History and Diff
git log # Show history
git log --oneline # Compact history
git diff # Show unstaged changes
git diff --staged # Show staged changes
git show <commit> # Show commit details
Undoing
git restore <file> # Discard changes
git restore --staged <file> # Unstage
git reset --soft HEAD~1 # Undo commit, keep changes
git reset --hard HEAD~1 # Undo commit, discard changes
git revert <commit> # Create undo commit
Remote
git remote -v # Show remotes
git remote add origin <url> # Add remote
git push -u origin main # First push
git pull origin main # Get changes
What You've Learned
Congratulations! You now know enough Git to be productive. Let's review:
Core Concepts
- [ ] Git tracks snapshots, not differences
- [ ] Three states: Working → Staged → Committed
- [ ] Commits are permanent snapshots with messages
Essential Commands
- [ ]
git init— Create repository - [ ]
git add— Stage changes - [ ]
git commit— Save permanently - [ ]
git status— Check what's happening - [ ]
git log— View history - [ ]
git diff— See changes
Branching
- [ ] Branches are parallel universes
- [ ]
git branchto create/ list - [ ]
git checkoutto switch - [ ]
git mergeto combine
Collaboration
- [ ]
git pushto share - [ ]
git pullto get updates - [ ]
git cloneto copy a repo
Safety
- [ ] How to undo mistakes
- [ ] How to resolve conflicts
- [ ] When to use
.gitignore
Next Steps
You've mastered the basics. Here's what to learn next:
Interactive Rebase — Clean up commit history before sharing
Cherry-picking — Apply specific commits to other branches
Stashing — Temporarily save uncommitted work
Git Hooks — Automate tasks with scripts
Git Flow — A branching strategy for teams
Forking Workflow — Contributing to open source
Resources
- — When things go wrong
— Free courses
Final Words of Wisdom
From someone who's used Git for over a decade:
You will make mistakes. Everyone does. The beauty of Git is that almost nothing is permanent. There's almost always a way to recover.
Commit often. A commit is a save point. Save early, save often. I commit every time I complete a small logical piece of work.
Write good commit messages. Future you will thank present you. A good message explains why, not just what.
Branch freely. Branches are cheap and easy. Create them for everything—features, bug fixes, experiments.
Pull before you push. Always get the latest changes before pushing your own. It avoids conflicts.
Git is a tool, not a religion. Use what works for you and your team. The "right" way is whatever keeps your code safe and your team happy.
That junior developer who cried over lost code? That was me. I've never lost code since learning Git. Now you won't either.
Enjoyed this tutorial? I'm **Alexander Merveille* from Marvelbiz Solutions, a Senior Software Engineer and 1-on-1 private Instructor. I write about development tools and workflows every Month. Follow me on X . And if you have a Git horror story, we've all been there!*
SOCIAL SHARE CARD GENERATOR