# Git Version Control

Git is a distributed version control system that tracks changes in source code during software development. It allows multiple developers to work together on non-linear development histories.

## 1. Key Concepts

*   **Repository (Repo):** The folder containing your project and the `.git` folder (which holds the version history).
*   **Commit:** A snapshot of your project at a specific point in time.
*   **Branch:** A parallel version of the repository. It allows you to work on features independently of the main codebase.
*   **Merge:** Combining changes from one branch into another.
*   **Remote:** A version of your repository hosted on the internet (e.g., GitHub, GitLab).

## 2. Basic Workflow

1.  **Modify** files in your working directory.
2.  **Stage** the files, adding snapshots of them to your staging area.
3.  **Commit** the staged files, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.

## 3. Essential Commands

### Setup & Initialization
*   `git init`: Initialize a new Git repository locally.
*   `git clone <url>`: Copy a remote repository to your local machine.
*   `git config --global user.name "Your Name"`: Configure your identity.

### Staging & Committing
*   `git status`: Check the status of your files (modified, staged, untracked).
*   `git add <file>`: Add a file to the staging area.
    *   `git add .`: Add all changed files.
*   `git commit -m "Message"`: Commit the staged changes with a descriptive message.

### Syncing
*   `git push`: Upload local commits to the remote repository.
*   `git pull`: Fetch and merge changes from the remote repository to your local machine.

## 4. Branching

Branching is Git's "killer feature". It allows you to diverge from the main line of development and continue to do work without messing with that main line.

*   `git branch`: List all local branches.
*   `git branch <name>`: Create a new branch.
*   `git checkout <name>` (or `git switch <name>`): Switch to a specific branch.
*   `git merge <branch>`: Merge the specified branch into the *current* branch.

## 5. .gitignore

A file named `.gitignore` specifies intentionally untracked files that Git should ignore (e.g., `node_modules`, `.env`, build artifacts).

## 6. Trunk Based Development

Trunk Based Development is a version control management practice where developers merge small, frequent updates to a core "trunk" or main branch.

*   **Core Idea:** Avoid long-lived feature branches.
*   **Workflow:** Developers work on short-lived branches (a few hours to a day) and merge back to `main` frequently.
*   **Benefits:** Reduces merge conflicts, enables Continuous Integration (CI), and faster feedback loops.
*   **Feature Flags:** Often used to hide unfinished features in production while still merging the code.

[[programming/linux-command-line-basics]]
[[programming/ci-cd-pipelines]]