> ## Documentation Index
> Fetch the complete documentation index at: https://programming-for-career.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Version Control Systems

> A comprehensive guide to version control systems, covering centralized and distributed models, Git commands, workflows, and best practices.

## Introduction

Version Control Systems (VCS) help developers track changes in code, collaborate efficiently, and manage software versions. They are crucial for modern software development, enabling teams to work on projects without overwriting each other's code.

## Types of Version Control Systems

### 1. Centralized Version Control System (CVCS)

A single central repository stores all versions of the code. Developers check out files, make changes, and commit them back.

**Examples:** SVN (Subversion), Perforce, TFS

**Workflow:**

```mermaid theme={null}
graph TD;
    Developer -->|Checkout| CentralRepo;
    Developer -->|Commit Changes| CentralRepo;
```

**Pros:**

* Simple structure
* Easier to manage permissions

**Cons:**

* Single point of failure (if the server is down, no one can access the repo)
* Requires constant internet connection

### 2. Distributed Version Control System (DVCS)

Each developer has a full copy of the repository, allowing offline work and better collaboration.

**Examples:** Git, Mercurial

**Workflow:**

```mermaid theme={null}
graph TD;
    LocalRepo1 -->|Push| RemoteRepo;
    RemoteRepo -->|Pull| LocalRepo2;
```

**Pros:**

* No single point of failure
* Supports offline work
* Better branching and merging capabilities

**Cons:**

* More complex than CVCS
* Requires disk space for local repositories

## Git: The Most Popular DVCS

Git is the most widely used Distributed Version Control System, allowing fast and efficient collaboration.

### Setting Up Git

#### Install Git:

* **Windows**: Download from [git-scm.com](https://git-scm.com/) and install.
* **macOS**: Use Homebrew:
  ```sh theme={null}
  brew install git
  ```
* **Linux**:
  ```sh theme={null}
  sudo apt install git # Debian/Ubuntu
  sudo yum install git # RHEL/CentOS
  ```

#### Configure Git:

```sh theme={null}
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
```

## Git Commands Explained

### Repository Management

```sh theme={null}
git init              # Initialize a new Git repository
git clone <repo_url>  # Clone an existing repository
```

### Staging and Committing Changes

```sh theme={null}
git add <file>        # Stage a specific file
git add .             # Stage all changes
git commit -m "Commit message"  # Commit staged changes
```

### Branching and Merging

```sh theme={null}
git branch            # List all branches
git branch <name>     # Create a new branch
git checkout <name>   # Switch to another branch
git merge <name>      # Merge a branch into the current one
```

### Working with Remote Repositories

```sh theme={null}
git remote add origin <repo_url>  # Connect to a remote repo
git push origin <branch>          # Push changes to remote
git pull origin <branch>          # Pull latest changes
```

### Undoing Changes

```sh theme={null}
git reset --soft HEAD~1  # Undo last commit, keep changes staged
git reset --hard HEAD~1  # Undo last commit, discard changes
```

## Real-Life Git Workflow Example

1. **Clone a repository:**
   ```sh theme={null}
   git clone https://github.com/example/project.git
   cd project
   ```
2. **Create a new branch:**
   ```sh theme={null}
   git checkout -b feature-login
   ```
3. **Make changes and commit:**
   ```sh theme={null}
   git add .
   git commit -m "Implemented user login feature"
   ```
4. **Push to remote repository:**
   ```sh theme={null}
   git push origin feature-login
   ```
5. **Create a pull request on GitHub/GitLab/Bitbucket.**

## Professional Commit Messages

A good commit message follows this format:

```
[Type] Short summary (Max 50 characters)

Detailed explanation of what was changed and why.
```

**Examples:**

```
feat: Add user authentication module
fix: Resolve login validation bug
refactor: Optimize database queries
```

## Conclusion

Version Control Systems are essential for managing software development efficiently. Git, as a DVCS, offers powerful features for tracking, collaborating, and maintaining code integrity. Mastering Git commands and workflows improves productivity and ensures smooth project management.

For further reading, visit the [official Git documentation](https://git-scm.com/doc).
