# Day 7 - Git and GitHub

## 🧠 **1\. What is Version Control System (VCS)?**

A **Version Control System** (VCS) helps developers:

1. **Share code** efficiently among team members.
    
2. **Track and manage changes** (versions) in code over time.
    

---

## 🧩 **2\. Why Do We Need Version Control?**

### 🧍‍♂️ Problem 1 — Sharing Code

Example:

* Developer 1 writes **addition** functionality.
    
* Developer 2 writes **subtraction** functionality.
    
* Both must combine their work to build a **calculator application**.
    

In real projects (like Amazon or Flipkart), there are **hundreds of files and dependencies**.  
Sharing and merging manually (through email, Slack, etc.) is **impossible**.

✅ **VCS helps developers share and merge code safely and efficiently.**

---

### 🧾 Problem 2 — Versioning

Example:

* Initially: “Addition of 2 numbers”
    
* Later: “Addition of 3 numbers” → “Addition of 4 numbers”
    
* Finally: Product manager wants to **revert to addition of 2 numbers**.
    

Without versioning, you cannot roll back to an older working version.

✅ **VCS lets you create, track, and revert code versions (v1, v2, v3, etc.).**

---

## 🧱 **3\. Types of Version Control Systems**

### 🧩 **Centralized Version Control (e.g., SVN, CVS)**

* A **single central server** stores all code.
    
* Developers (Dev1, Dev2, etc.) **commit and pull** changes from that server.
    
* If the central server goes down → **no one can collaborate**.
    

```plaintext
 Dev1  ↔
        ↘
          Central Server (SVN)
        ↗
 Dev2  ↔
```

❌ **Single point of failure**  
❌ **Limited collaboration when offline**

---

### 🌍 **Distributed Version Control (e.g., Git)**

* Every developer has a **full local copy** of the repository.
    
* Developers can **commit, work, and roll back** locally.
    
* Later, changes can be **pushed** to a shared remote repository (e.g., GitHub).
    

```plaintext
       ┌──────────────┐
       │ Central Repo │
       └──────┬───────┘
              │
     ┌────────┴────────┐
     │                 │
 Local Repo 1     Local Repo 2
 (Dev1)             (Dev2)
```

✅ No single point of failure  
✅ Work offline and sync anytime  
✅ Faster operations

---

### 💡 **Interview Tip**

**Q:** What is the difference between centralized and distributed VCS?  
**A:** Centralized has one main server (like SVN); distributed gives each developer a full local copy (like Git).

---

## 🔁 **4\. What is a Fork?**

* A **Fork** is a **personal copy** of a repository.
    
* Developers use forks to make changes without affecting the original codebase.
    

Example:  
Company’s main repo → `example/project`  
Your fork → `yourname/project`

✅ You can modify freely and later **merge** back via a **Pull Request**.

---

## 🧰 **5\. What is Git?**

> Git is an **open-source distributed version control system**.

* You can install Git on any server (e.g., EC2 instance or local machine).
    
* It tracks changes, manages versions, and supports collaboration.
    
* Commands like `git add`, `git commit`, and `git push` form the **Git lifecycle**.
    

---

## 🌐 **6\. What is GitHub?**

> GitHub is a **cloud-based platform built on top of Git** that adds:

* UI and collaboration features
    
* Issue tracking
    
* Code reviews and pull requests
    
* CI/CD and project management tools
    

You can:

* Create repositories
    
* Host projects
    
* Collaborate with teams
    
* Manage workflows visually
    

---

### 🧩 Git vs GitHub — Key Difference

| Feature | Git | GitHub |
| --- | --- | --- |
| Type | Tool (VCS) | Platform (built on Git) |
| Usage | Local versioning | Online hosting & collaboration |
| Ownership | Open source | Owned by Microsoft |
| Access | Command line | Web UI + CLI |
| Example | Installed on local/EC2 | GitHub.com / GitHub Enterprise |

---

## ⚙️ **7\. Installing Git**

### 🔹 For Linux:

```plaintext
sudo apt update
sudo apt install git -y
```

### 🔹 For Windows/Mac:

Download from 👉 [https://git-scm.com/downloads](https://git-scm.com/downloads)

✅ Verify installation:

```plaintext
git --version
```

---

## 💻 **8\. Basic Git Lifecycle & Commands**

| Step | Command | Purpose |
| --- | --- | --- |
| Initialize Repository | `git init` | Create a new local Git repository |
| Check Status | `git status` | Show current changes |
| Add File(s) | `git add <filename>` | Stage files for commit |
| Commit Changes | `git commit -m "message"` | Save staged files as a version |
| View History | `git log` | Show list of commits |
| Compare Changes | `git diff` | See what changed in files |
| Push to Remote | `git push origin main` | Upload local commits to GitHub |

---

## 🧩 **9\. How Git Tracks Changes**

* Git creates a hidden folder `.git/` when you run `git init`.
    
* It contains:
    
    * `objects/` → data storage (commits, blobs)
        
    * `refs/` → references to branches
        
    * `config` → repository configuration
        
    * `hooks/` → automation scripts
        
    * `HEAD` → pointer to current branch
        

If `.git/` is deleted → repo tracking is lost.

---

## 📘 **10\. Example Workflow**

```plaintext
# Step 1: Create folder
mkdir example && cd example

# Step 2: Initialize Git
git init

# Step 3: Create a file
echo "a+b" > calculator.sh

# Step 4: Check status
git status

# Step 5: Track file
git add calculator.sh

# Step 6: Commit
git commit -m "Initial version - addition"

# Step 7: Modify file
echo "a+b+c" > calculator.sh

# Step 8: Check difference
git diff

# Step 9: Commit new version
git commit -am "Updated addition to include 3 numbers"

# Step 10: View commit history
git log
```

✅ You can revert to any previous version using:

```plaintext
git reset --hard <commit_id>
```

---

## ☁️ **11\. Connecting to GitHub**

1. Create a **GitHub account** at [https://github.com](https://github.com)
    
2. Click **New Repository**
    
3. Name it → e.g., `calculator`
    
4. Choose **Public** or **Private**
    
5. Click **Create Repository**
    

Now connect your local repo:

```plaintext
git remote add origin https://github.com/<username>/<repo>.git
git branch -M main
git push -u origin main
```

Your project is now hosted on GitHub 🌍

---

## 🧱 **12\. Real-World Collaboration Workflow**

1. **Fork** the main repo (copy)
    
2. **Clone** to your local system
    
3. Make changes
    
4. Commit and push to your fork
    
5. Create a **Pull Request (PR)** to merge into the main branch
    

---

## 🧩 **13\. Summary**

| Concept | Purpose |
| --- | --- |
| **Version Control** | Manage code versions and track changes |
| **Centralized VCS (SVN)** | Single server, limited collaboration |
| **Distributed VCS (Git)** | Full local copies, better reliability |
| **Git** | Core tool for version control |
| **GitHub** | Collaboration platform built on Git |
| **Fork** | Copy of a repository |
| **Commit** | Saved version of code |
| **Push/Pull** | Upload/download code from remote repo |

---

✅ **In short:**

> Git manages your code versions locally.  
> GitHub lets you share and collaborate globally.
