> ## Documentation Index
> Fetch the complete documentation index at: https://genai.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Version control

> Understanding Git basics

## The problem Git solves

## Why Do We Need Version Control?

Imagine developing a project without Git.

To keep different versions, you might create folders like:

```text theme={null}
StudentApp/
StudentApp_Final/
StudentApp_Final_v2/
StudentApp_Final_Latest/
StudentApp_Final_Final/
```

After some time, it becomes difficult to answer questions like:

* Which version is the latest?
* Which version introduced a bug?
* How can I restore an earlier working version?

A **Version Control System (VCS)** solves these problems by maintaining the complete history of your project.

## What is Version Control?

Version Control is a system that records changes made to files over time.

It helps you:

* Track changes
* Restore previous versions
* Compare versions
* Collaborate with others
* Maintain the complete history of a project

Instead of creating multiple copies of a project manually, a Version Control System manages the history automatically.

## Types of Version Control

### Local Version Control

History is stored only on your computer.

**Advantages**

* Fast
* Simple

**Limitations**

* No collaboration
* History is lost if the computer fails

### Centralized Version Control

A central server stores the project history.

**Advantages**

* Easy collaboration
* Single source of truth

**Limitations**

* Requires network connectivity
* Server failure affects everyone

**Examples**

* SVN
* CVS

### Distributed Version Control

Every developer has a complete copy of the repository.

**Advantages**

* Works offline
* Full history on every machine
* Faster operations
* Better collaboration

**Git** is a Distributed Version Control System (DVCS).

## What is Git?

Git is a **Distributed Version Control System** that tracks changes by storing snapshots of your project.

Git allows you to:

* Track changes
* Save versions (commits)
* Restore previous versions
* Create branches
* Merge changes
* Collaborate with others

Git runs on your local computer and can be used without GitHub.

## What is GitHub?

GitHub is a cloud platform that hosts Git repositories.

| Git                        | GitHub                         |
| -------------------------- | ------------------------------ |
| Version Control System     | Repository Hosting Platform    |
| Installed on your computer | Cloud service                  |
| Works offline              | Requires internet              |
| Creates commits            | Stores and shares repositories |

Other Git hosting platforms include:

* GitLab
* Bitbucket
* Azure DevOps

## Project

A project is simply a folder containing your source code.

```text theme={null}
StudentApp/
│
├── app.py
├── login.py
├── README.md
└── requirements.txt
```

Initially, Git does not track this folder.

## Working Directory

The **Working Directory** is your project folder where you create, edit, rename, and delete files.

```text theme={null}
StudentApp/
│
├── app.py
├── login.py
└── README.md
```

These are the files you work with every day.

## Repository

A **Repository (Repo)** is Git's database that stores the history and metadata of your project.

There are two types of repositories.

### Local Repository

The repository stored on your computer.

Created using:

```bash theme={null}
git init
```

Stored inside the hidden folder:

```text theme={null}
.git/
```

The Local Repository stores:

* Commit history
* Branches
* Tags
* Repository configuration
* Other Git metadata

### Remote Repository

A copy of the Local Repository hosted on a server such as:

* GitHub
* GitLab
* Bitbucket

It is mainly used for:

* Backup
* Collaboration
* Sharing code

## Creating a Local Repository

Suppose your project is:

```text theme={null}
StudentApp/
│
├── app.py
└── login.py
```

Initialize Git:

```bash theme={null}
git init
```

Git creates a hidden folder.

```text theme={null}
StudentApp/
│
├── app.py
├── login.py
└── .git/
```

The `.git` folder is your **Local Repository**.

> **Note**
>
> `git init` only creates an empty Git repository. It does **not** copy your project files into the repository or create any commits.

## Before and After `git init`

### Before

```text theme={null}
Working Directory
-----------------
app.py
login.py
```

Only your project files exist.

### After

```text theme={null}
Working Directory
-----------------
app.py
login.py

Repository (.git)
-----------------
Empty
```

The repository now exists, but it contains:

* No commits
* No version history
* No tracked snapshots

Your files are still only in the **Working Directory**.

## Local Repository vs Remote Repository

```text theme={null}
                   git push
Local Repository -------------> Remote Repository
      (.git)                        (GitHub)

                   git pull
Local Repository <------------- Remote Repository
```

* The **Local Repository** stores your project's history on your computer.
* The **Remote Repository** is a shared copy used for collaboration and backup.
* GitHub stores a copy of your Local Repository, not your Working Directory.

## Common Git Commands

| Command      | Purpose                                               |
| ------------ | ----------------------------------------------------- |
| `git init`   | Create a new local repository                         |
| `git status` | Show the current state of your project                |
| `git add`    | Stage files for the next commit                       |
| `git commit` | Save a new version of the project                     |
| `git log`    | View commit history                                   |
| `git push`   | Upload commits to the remote repository               |
| `git pull`   | Download and merge changes from the remote repository |

## Summary

| Term              | Description                                       |
| ----------------- | ------------------------------------------------- |
| Version Control   | Maintains the history of files                    |
| Git               | Distributed Version Control System                |
| GitHub            | Cloud platform for hosting Git repositories       |
| Project           | Folder containing source code                     |
| Working Directory | Editable project files                            |
| Repository        | Git database storing project history and metadata |
| Local Repository  | `.git` folder on your computer                    |
| Remote Repository | Repository hosted on GitHub or similar platforms  |

## Key Takeaways

* Git is a **Version Control System**, while GitHub is a **repository hosting platform**.
* A project starts as a normal folder.
* Running `git init` creates a hidden `.git` folder called the **Local Repository**.
* After `git init`, the repository exists but is still **empty**.
* Your project files remain in the **Working Directory** until they are staged and committed.
* The **Remote Repository** is simply a copy of your **Local Repository** hosted online.
