What is Git? 🎨¶
Introduction¶
Git is a distributed version control system created by Linus Torvalds in 2005 (yes, the same person who created Linux!). It's designed to handle everything from small to very large projects with speed and efficiency.
Git in Simple Terms¶
Think of Git as a sophisticated time machine for your files that:
- 📸 Takes snapshots of your project at different points in time
- 🔄 Lets you travel back to any snapshot
- 🌿 Allows you to create parallel timelines (branches)
- 🤝 Helps multiple people work together without chaos
The Git Workflow¶
Git has a three-stage architecture that gives you precise control over what gets saved in your project history.
The Three States¶
Every file in Git exists in one of three states:
1. 📁 Working Directory¶
This is your workspace - where you actually edit files.
# Your project folder
my-project/
├── index.html ← Edit this file
├── style.css ← Make changes here
└── script.js ← Work on your code
Working Directory
- Where you make changes to files
- Just a normal folder on your computer
- Changes here are NOT tracked yet
2. 📋 Staging Area (Index)¶
This is the preparation zone - where you choose which changes to include in your next commit.
# You edited 5 files, but only want to commit 3
$ git add index.html style.css
# Now these 2 files are "staged" and ready to commit
Why Staging?
The staging area lets you craft perfect commits. You can:
- Group related changes together
- Commit features one at a time
- Review changes before committing
- Leave unfinished work unstaged
3. 🗄️ Repository (Local Repo)¶
This is your permanent history - where Git stores all committed snapshots.
$ git commit -m "Add login feature"
# Your staged changes are now permanently saved in Git's history
Repository
- Complete project history
- Stored in the hidden
.gitfolder - Can recover any previous version
- Shareable with others
The Basic Git Workflow¶
Here's how the typical workflow looks:
Step-by-Step Example¶
Status: Changes are in Working Directory ⚠️ (not tracked)
Status: Changes are Committed 🎉 (saved in history)
Visualizing the Workflow¶
graph LR
A[Working<br/>Directory] -->|git add| B[Staging<br/>Area]
B -->|git commit| C[Repository]
C -->|git checkout| A
style A fill:#fff4e6,stroke:#ff9800,stroke-width:3px
style B fill:#e8f5e9,stroke:#4caf50,stroke-width:3px
style C fill:#e3f2fd,stroke:#2196f3,stroke-width:3px
Real-World Example¶
Let's say you're building a website and working on multiple features:
Scenario: Multiple Changes¶
# You made changes to 4 files
$ git status
Modified: header.html
Modified: footer.html
Modified: login.js
Modified: debug.js
Option 1: Commit Everything (Not Recommended)¶
Option 2: Selective Staging (Best Practice)¶
# Commit related changes together
$ git add header.html footer.html
$ git commit -m "Update navigation menu styling"
$ git add login.js
$ git commit -m "Fix login validation bug"
# Leave debug.js unstaged (still testing)
Clean History
Now your history is clear, organized, and easy to understand!
Key Git Concepts¶
Commits: Snapshots, Not Diffs¶
Git stores complete snapshots of your project, not just the differences.
The .git Directory¶
When you initialize a Git repository, Git creates a hidden .git folder:
my-project/
├── .git/ ← Git's "brain" (don't touch!)
│ ├── objects/ ← All your snapshots
│ ├── refs/ ← Branches and tags
│ ├── HEAD ← Current branch pointer
│ └── config ← Repository settings
├── index.html
└── style.css
Never Delete .git!
The .git folder contains your entire project history. Deleting it means losing all Git data!
Git's Data Model¶
Everything is a Commit¶
gitGraph
commit id: "A: Initial commit"
commit id: "B: Add feature"
commit id: "C: Fix bug"
commit id: "D: Update docs"
Each commit contains:
Commit: a3f5d9c
Author: Jane Doe <jane@example.com>
Date: Mon Oct 30 14:32:18 2025
Message: "Add login feature"
Changes:
- login.html (new file)
- app.js (modified)
Parent: b2e4c8a ← Points to previous commit
Commits Form a Timeline¶
Each commit knows its parent, creating an unbreakable chain of history.
What Makes Git Special?¶
1. Speed ⚡¶
Most operations are local (no network needed):
2. Branching 🌿¶
Create branches in milliseconds:
$ git branch feature-login # Creates a branch
$ git checkout feature-login # Switches to it
# Or both at once:
$ git checkout -b feature-login
3. Distributed 🌍¶
Every developer has the full history:
Developer 1 Computer → Full history
Developer 2 Computer → Full history
GitHub Server → Full history
4. Data Integrity 🔒¶
Every file and commit is checksummed:
commit a3f5d9c847e829013abc9873...
# This hash is calculated from the commit contents
# Any change = different hash
# Impossible to corrupt without Git knowing
5. Non-Linear Development 🔀¶
Work on multiple features simultaneously:
gitGraph
commit
commit
branch feature-A
checkout feature-A
commit
commit
checkout main
branch feature-B
checkout feature-B
commit
checkout main
merge feature-A
checkout feature-B
commit
checkout main
merge feature-B
Git Commands Overview¶
Here's a quick preview of essential commands (we'll learn these in detail later):
Setup & Configuration¶
Creating Repositories¶
Basic Workflow¶
git status # Check what's changed
git add <file> # Stage changes
git commit -m "Message" # Save snapshot
History & Exploration¶
Try It Yourself! 🎮¶
Exercise 1: Create Your First Repository¶
Open your terminal and try this:
# Create a new directory
$ mkdir my-first-repo
$ cd my-first-repo
# Initialize Git
$ git init
# Output: Initialized empty Git repository in /path/to/my-first-repo/.git/
# Check status
$ git status
# Output: On branch main
# No commits yet
# nothing to commit
Congratulations!
You just created your first Git repository! The .git folder now exists (it's hidden, so use ls -la to see it).
Exercise 2: Understand the Three States¶
# Create a file (Working Directory)
$ echo "Hello Git!" > hello.txt
$ git status
# Output: Untracked files: hello.txt
# Stage the file (Staging Area)
$ git add hello.txt
$ git status
# Output: Changes to be committed: new file: hello.txt
# Commit (Repository)
$ git commit -m "Add greeting file"
$ git status
# Output: nothing to commit, working tree clean
Common Questions¶
Why do I need the staging area? Can't I just commit directly?
The staging area gives you control! You can:
- Commit only related changes together
- Review changes before committing
- Keep work-in-progress unstaged
- Create clean, logical commit history
Many Git tools allow skipping staging (git commit -a), but understanding it makes you a better Git user.
What's the difference between Git and GitHub?
- Git = Version control software (runs on your computer)
- GitHub = Website for hosting Git repositories (in the cloud)
Think of it like: - Git = Microsoft Word - GitHub = Google Docs
We'll cover GitHub in detail later!
Can I use Git for non-code projects?
Absolutely! Git works great for:
- Documentation (Markdown, LaTeX)
- Design files (if text-based)
- Configuration files
- Writing (books, articles)
- Any text files that change over time
How much space does Git use?
Git is surprisingly efficient! It:
- Compresses data
- Stores only changes (internally)
- Removes duplicates
A repo with years of history might only be a few MB.
What's Next?¶
Now that you understand what Git is and its three-stage workflow, let's dive deeper into the actual workflow in practice.
Key Takeaways
- ✅ Git is a distributed version control system
- ✅ Three stages: Working Directory → Staging Area → Repository
- ✅
git addmoves changes to staging - ✅
git commitsaves staged changes permanently - ✅ Every commit is a complete snapshot
- ✅ Git is fast, efficient, and reliable