Git & GitHub: The Survival Guide I Needed As a Beginner
Everything that confused me about Git, explained the way I wish someone had explained it to me.
Git Confused Me Too. Let's Fix That.
I'm going to be real with you: Git made absolutely no sense to me when I started. I'd been programming for months, and every tutorial just threw commands at me. "Run git add." "Now git commit." "Push to origin." But nobody explained WHY.
After years of using Git daily (and after helping dozens of developers understand it), I want to give you the explanation I wish I had.
First, What Problem Does Git Actually Solve?
Imagine you're writing a document. At some point, you save a copy called "report_v2.doc". Then "report_final.doc". Then "report_FINAL_FINAL.doc". Sound familiar?
Now imagine you're working on that document with three other people. Everyone has their own copy. Someone makes changes on Tuesday, someone else makes different changes on Wednesday. How do you combine everyone's work without losing anything?
That's what Git does. It's a system that:
- Tracks every change you make to your files
- Lets you go back in time to any previous version
- Allows multiple people to work on the same project without overwriting each other
GitHub is something different: it's a website where you can store your Git projects online and collaborate with others. Git is the tool; GitHub is a service that hosts your Git projects.
The Three Stages (This Is Key!)
Here's what took me the longest to understand. Git has three "stages" for your files:
- Working Directory - Your actual files that you're editing
- Staging Area - A preparation zone for your next save point
- Repository - The permanent history of all your saves
Think of it like packing for a trip:
- Working Directory = All your clothes scattered on your bed
- Staging Area = The suitcase you're packing
- Repository = The suitcase, fully packed and zipped, ready to go
Your First Git Project: Step by Step
Let's create a project and start using Git. Open your terminal and follow along:
# 1. Create a new folder
mkdir my-first-project
cd my-first-project
# 2. Initialize Git (this creates a hidden .git folder)
git init
# Now you have a Git repository! But it's empty.
Create a file called README.md and add some text. Now let's save this to Git:
# 3. Check what files Git sees
git status
# You'll see README.md in red - it's "untracked"
# 4. Add the file to staging (the suitcase)
git add README.md
git status
# Now README.md is in green - it's staged
# 5. Commit (zip up the suitcase with a label)
git commit -m "Add README file"
That's it! You've made your first commit.
Commit Messages: What I've Learned the Hard Way
Bad commit messages I've written:
- "fix"
- "update"
- "asdfasdf"
Better commit messages:
- "Fix login button not responding on mobile"
- "Add user profile page with avatar upload"
Start with a verb. Be specific. Your teammate (or future you) should understand what the change does without opening the code.
Branches: Working Without Breaking Things
Here's a scenario: you want to try adding a new feature, but you don't want to mess up your working code. That's what branches are for.
# Create and switch to a new branch
git checkout -b add-contact-page
# Now you're on the "add-contact-page" branch
# Any changes you make won't affect "main"
Think of branches as parallel universes. In one universe (main), your site is working perfectly. In another universe (add-contact-page), you're experimenting. If it works, merge it into the main universe.
# After you're done with your feature
git add .
git commit -m "Add contact page with form"
# Switch back to main
git checkout main
# Merge your feature branch into main
git merge add-contact-page
The Most Common "Oh No" Moments
"I made changes to the wrong branch!"
# Save your changes temporarily
git stash
# Switch to the correct branch
git checkout correct-branch
# Apply your changes here
git stash pop
"I want to undo my last commit!"
# Undo commit but keep the changes
git reset --soft HEAD~1
# Undo commit AND throw away changes (careful!)
git reset --hard HEAD~1
Commands You'll Use 90% of the Time
git status # What's changed?
git add . # Stage everything
git commit -m "message" # Save changes
git push # Upload to GitHub
git pull # Download from GitHub
git checkout -b branch-name # Create new branch
git checkout main # Switch to main
git merge branch-name # Combine branches
git log --oneline # See commit history
That's it. There are hundreds of Git commands, but these cover most of what you'll do.
One Last Piece of Advice
You're going to mess up. Everyone does. I once accidentally pushed API keys to GitHub (had to rotate them immediately). I've force-pushed over a colleague's work.
The nice thing about Git is that almost nothing is permanent. If you made a commit, it's saved forever unless you actively delete it. You can almost always go back.
Start simple. Clone a repository, make changes, push them. Then try branching. Each new concept will click with practice.
Happy coding!