🔀What is Git? (Version Control)
Track changes, undo mistakes, and collaborate — Git explained simply
What is Git? (Version Control)
The Problem: Why Do We Need Version Control?
Imagine you are writing a school essay. You save it as `essay.doc`. Then you make changes and save it as `essay_v2.doc`. Then more changes: `essay_final.doc`. Then: `essay_final_FINAL.doc`. Then: `essay_final_FINAL_v2_REALLY_FINAL.doc`.
Sound familiar? This is chaos.
Now imagine 5 people editing the same essay at the same time. How do you know who changed what? How do you undo a bad change? How do you combine everyone's work without losing anything?
This is the problem version control solves.
What is Version Control?
Version control is a system that tracks every change made to your files over time.
Think of it like Google Docs history — but for code:
``` Google Docs History Version Control (Git) ───────────────── ───────────────────── You can see who You can see who changed what changed what
You can go back You can go back to any version to any version
Multiple people Multiple people edit at once edit at once
Automatic saves Manual saves (commits) = more control ```
The key difference: with Git, you decide when to save a snapshot. Each save is called a commit, and it includes a message describing what changed.
Why You Need Version Control
Here are the top reasons every developer uses version control:
1. Undo Mistakes (Time Machine)
Made a mistake? Go back to any previous version instantly.
``` Today: "Oops, I broke everything!" With Git: "No problem, let me go back to yesterday's version." ```
2. Backup Your Work
Your code lives in the cloud. Even if your laptop breaks, your code is safe.
3. Collaboration
Multiple developers can work on the same project without overwriting each other's work.
``` Developer A: Working on the login page Developer B: Working on the dashboard Developer C: Fixing a bug in the footer
All working at the same time.
All changes get combined safely.
```
4. Track History
See exactly what changed, when, and by whom.
``` commit abc123 — "Add login form validation" — by Sarah — Jan 15 commit def456 — "Fix button color on mobile" — by Ahmed — Jan 14 commit ghi789 — "Create user registration" — by Sarah — Jan 13 ```
5. Experiment Safely
Try new ideas without risking your working code. If the experiment fails, just throw it away.
What is Git?
Git is the most popular version control system in the world.
- Created in 2005 by Linus Torvalds (the creator of Linux)
- Used by almost every software company on earth
- It is free and open source
- It runs on your local computer (no internet needed for basic use)
Git is a command-line tool. You interact with it by typing commands in your terminal.
Git is NOT GitHub
This is a very common confusion. Let's clear it up:
``` ┌──────────────────────────────────────────────────┐ │ │ │ Git ≠ GitHub │ │ │ │ Git = The tool (runs on your computer) │ │ GitHub = A website (stores Git repos online) │ │ │ │ Analogy: │ │ Git = The video camera │ │ GitHub = YouTube (where you upload videos) │ │ │ │ You can use Git WITHOUT GitHub. │ │ You CANNOT use GitHub WITHOUT Git. │ │ │ └──────────────────────────────────────────────────┘ ```
What is GitHub / GitLab / Bitbucket?
These are cloud hosting platforms for Git repositories. Think of them as Google Drive for code.
| Platform | Description |
|---|---|
| GitHub | Most popular. Owned by Microsoft. |
| GitLab | Popular for companies. Self-hosted. |
| Bitbucket | Popular with Atlassian/Jira users. |
They all use Git underneath. The difference is the website and extra features they offer (pull requests, issues, CI/CD, etc.).
Key Git Concepts
Let's learn the vocabulary of Git. These are the building blocks:
1. Repository (Repo)
A repository is your project folder, tracked by Git.
``` my-project/ <-- This is a repository ├── .git/ <-- Hidden folder where Git stores history ├── index.html ├── style.css └── script.js ```
When you run `git init` inside a folder, it becomes a Git repository.
2. Commit
A commit is a snapshot of your project at a specific point in time.
``` Timeline of commits: ─────────────────────────────────────────────►
📸 Commit 1 📸 Commit 2 📸 Commit 3 "Initial setup" "Add header" "Fix typo" Jan 1 Jan 2 Jan 3 ```
Each commit has:
- A unique ID (hash like `a1b2c3d`)
- A message describing the change
- The author name
- A timestamp
3. Branch
A branch is a parallel version of your project.
``` ┌── 📸 ── 📸 ── 📸 (feature-login branch) │ 📸 ── 📸 ── 📸 ──┤ (main branch) │ └── 📸 ── 📸 (fix-bug branch) ```
- The main branch is the "official" version
- You create branches to work on features or fixes
- When done, you merge the branch back into main
4. Merge
Merging combines changes from one branch into another.
``` Before merge: main: A ── B ── C feature-login: A ── B ── D ── E
After merge: main: A ── B ── C ── F (F combines C + D + E) ```
5. Remote
A remote is the cloud copy of your repository (e.g., on GitHub).
``` ┌─────────────────┐ ┌──────────────────┐ │ Your Computer │ │ GitHub │ │ (local repo) │ ──────► │ (remote repo) │ │ │ push │ │ │ │ ◄────── │ │ │ │ pull │ │ └─────────────────┘ └──────────────────┘ ```
- push = send your commits to the cloud
- pull = download new commits from the cloud
- clone = copy a remote repo to your computer for the first time
Installing Git
On macOS
Open Terminal and type:
```bash
Check if Git is already installed
git --version
If not installed, install via Homebrew
brew install git ```
On Windows
Download from: https://git-scm.com/download/win
Or use winget:
```bash winget install --id Git.Git -e --source winget ```
On Linux (Ubuntu/Debian)
```bash sudo apt update sudo apt install git ```
Verify Installation
```bash git --version
Output: git version 2.42.0 (or similar)
```
Configuring Git
After installing Git, you need to tell it who you are. This information appears in every commit you make.
```bash
Set your name
git config --global user.name "Your Name"
Set your email
git config --global user.email "your.email@example.com"
Verify your settings
git config --global --list ```
Why is this important?
Every commit records the author. When your team looks at the history, they see:
``` commit a1b2c3d Author: Your Name your.email@example.com Date: Mon Jan 15 10:30:00 2024
Add login form validation
```
Other Useful Config
```bash
Set default branch name to "main" (instead of "master")
git config --global init.defaultBranch main
Set default editor (for commit messages)
git config --global core.editor "code --wait" # VS Code ```
What is .gitignore?
A `.gitignore` file tells Git which files to ignore — files that should NOT be tracked.
Why ignore files?
Some files should never be in your repository:
| Type | Example | Why ignore? |
|---|---|---|
| Dependencies | `node_modules/` | Too large, can be re-downloaded |
| Secrets | `.env` | Contains passwords and API keys |
| Build output | `dist/`, `build/` | Generated files, not source |
| OS files | `.DS_Store` | System files, not project files |
| Editor files | `.vscode/` | Personal editor settings |
Example .gitignore file
```
Dependencies
node_modules/
Environment variables (secrets!)
.env .env.local
Build output
dist/ build/
OS files
.DS_Store Thumbs.db
Editor files
.vscode/ .idea/ ```
How to create .gitignore
Create the file in the root of your project:
```bash
Create the file
touch .gitignore
Edit it with your editor and add your rules
```
Tip: Always create `.gitignore` BEFORE your first commit. If you already committed files you want to ignore, you need extra steps to remove them from tracking.
Visual Summary: The Git Workflow
``` ┌──────────────┐ git add ┌──────────────┐ git commit ┌──────────────┐ │ │ ──────────────► │ │ ───────────────► │ │ │ Working │ │ Staging │ │ Repository │ │ Directory │ │ Area │ │ (.git) │ │ │ │ │ │ │ │ (your files │ │ (files │ │ (permanent │ │ as you │ │ ready to │ │ snapshots) │ │ edit them) │ │ be saved) │ │ │ └──────────────┘ └──────────────┘ └──────────────┘ │ git push │ ▼ ┌──────────────┐ │ Remote │ │ (GitHub) │ └──────────────┘ ```
- You edit files in your working directory
- You stage the files you want to save (`git add`)
- You commit the staged files (`git commit`)
- You push commits to the cloud (`git push`)
Quick Reference: Essential Git Commands
| Command | What it does |
|---|---|
| `git init` | Create a new repository |
| `git clone <url>` | Copy a remote repo to your computer |
| `git status` | See what changed |
| `git add <file>` | Stage a file for commit |
| `git add .` | Stage all changed files |
| `git commit -m "message"` | Save a snapshot with a message |
| `git push` | Upload commits to remote |
| `git pull` | Download commits from remote |
| `git log` | View commit history |
| `git branch` | List branches |
| `git checkout -b <name>` | Create and switch to a new branch |
| `git merge <branch>` | Merge a branch into current branch |
Key Takeaways
- Version control tracks every change to your files over time
- Git is the most popular version control tool — it runs locally on your computer
- GitHub is a cloud platform for hosting Git repositories — it is NOT Git itself
- A repository is a project folder tracked by Git
- A commit is a snapshot (save point) of your project
- A branch lets you work on features without affecting the main code
- Merging combines changes from different branches
- A remote is the cloud copy of your repo
- `.gitignore` tells Git which files to skip
- Always configure `user.name` and `user.email` before your first commit
What's Next?
In the next lesson, you will learn the hands-on Git workflow: how to create a repository, make commits, create branches, and push to GitHub.