# for Beginners: Basics and Essential Commands

## What is Git?

Git serves as a distributed version control system that tracks changes in your code, enabling multiple developers to collaborate efficiently without overwriting each other’s work. Before Git, developers faced issues like manually copying code via pendrives or email, leading to version confusion, lost changes, and merge nightmares when combining files.

### Why Use Git

Git eliminates the chaos of traditional methods by creating snapshots of your project at any point, allowing you to revert changes, experiment safely, and maintain a complete history locally. Its distributed nature means every developer has a full copy of the repository, reducing reliance on central servers and enabling offline work.

![Git Gud: The Working Tree, Staging Area, and Local Repo | by Lucas Maurer |  Medium](https://miro.medium.com/1*diRLm1S5hkVoh5qeArND0Q.png align="left")

### Core Concepts

* **Repository (Repo):** A directory containing your project files and their complete revision history, either local or remote.
    
* **Working Directory:** Your everyday files where you edit code; changes here aren’t tracked until staged.  
    Staging Area (Index): Intermediate zone where you select specific changes to prepare for a commit.
    
* **Commit:** A saved snapshot of staged changes with a unique ID and descriptive message, forming your project’s history.
    
* **Branch:** A lightweight pointer to a series of commits, letting you work on features independently from the main codebase.
    
* **HEAD:** Points to the current commit or branch you’re on, moving forward as you commit.
    

### Basic Workflow

Start with an empty folder for your project.

* Initialize:  `git init`  creates a new repo with a  `.git`  folder.
    
* Check status:  `git status`  shows modified, staged, or untracked files.
    
* Stage changes:  `git add filename.txt`  (or  `git add .`  for all) moves files to staging.
    
* Commit:  `git commit -m "Initial project setup"` saves the snapshot.
    
* View history:  `git log`  displays commits with IDs, authors, and messages.
    
* Create branch:  `git branch feature-x`  then  `git checkout feature-x`  (or  `git switch feature-x` ) to work separately.
    
* Merge: Switch to main ( `git checkout main` ), then  `git merge feature-x`  to combine.
    

This workflow mirrors real development: edit in working directory → stage selectively → commit to repo → branch for features → merge when ready.

![Understanding Git Repository - GeeksforGeeks](https://media.geeksforgeeks.org/wp-content/uploads/20250710172654460719/What-is-Git-repository.webp align="left")

### Common Commands

| **Command** | **Purpose** | **Example** |
| --- | --- | --- |
| `git init` | Start new repo | `git init` |
| `git status` | Check file states | `git status` |
| `git add` | Stage changes | `git add .` |
| `git commit` | Save snapshot | `git commit -m "msg"` |
| `git log` | View history | `git log --oneline` |
| `git branch` | List/manage branches | `git branch -d old-branch` |
| `git checkout` | Switch branches/commits | `git checkout -b new-branch` |

### Workflow Example

Start with ChaiCode portfolio/tutorial site:

1\. Create folder  `chai-code`  and navigate:  cd `chai-code .`  
2\. `git init`  – Initializes repo for your ChaiCode project.  
3\. Add files: Create  `index.html`  (homepage),  `app.js`  (basic JS logic),  styles.css .  
4\. Check:  `git status`  reveals untracked files in your ChaiCode directory.  
5\. Stage selectively:  `git add index.html app.js`  (skip CSS for now).  
6\. Commit:  `git commit -m "Initial ChaiCode homepage and JS"` .  
7\. Iterate: Edit  `app.js`  to add a “Brew Code” button function, then  `git add app.js` ,  `git commit -m "Add interactive brewing feature"` .  
8\. History:  `git log --oneline`  shows clean ChaiCode evolution like  `e4f5g6 (HEAD -> main) Add brewing feature` .

![Git: How to Write Commits and Why](https://www.develer.com/wp-content/uploads/2021/08/image-9.png align="left")
