# Git for Beginners: Basics and Essential Commands

1. ## 1\. What is Git?
    
    Git is a **distributed version control system**. Think of it as a **time machine for your code**:
    
    * It tracks every change you make to your files.
        
    * It lets you save different versions of your project.
        
    * It helps teams work together without overwriting each other’s work.
        
    
    **Key Point:** Git is **not** GitHub/GitLab—those are online platforms that *host* Git repositories.
    
    ---
    
    ## 2\. Why Use Git?
    
    * **Track Changes:** See who made what change and when.
        
    * **Collaborate:** Work with others on the same project without conflicts.
        
    * **Backup:** Never lose code—restore old versions anytime.
        
    * **Experiment:** Try new ideas in branches; discard or merge them later.
        
    
    ---
    
    ## 3\. Git Basics: Core Terms
    
    | Term | What It Means |
    | --- | --- |
    | **Repository (Repo)** | Your project folder where Git tracks changes. |
    | **Commit** | A saved snapshot of your project at a point in time. |
    | **Branch** | A parallel version of your project (e.g., `main`, `feature/login`). |
    | **HEAD** | A pointer to your current branch/commit (like a bookmark). |
    | **Stage** | Preparing changes to be committed (using `git add`). |
    | **Remote** | A copy of your repo on a server (e.g., GitHub). |
    
    ---
    
    ## 4\. Essential Git Commands
    
    ### Start a Project
    
    ```plaintext
    git init          # Turns your folder into a Git repo
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769628006316/0aed5428-bc8f-4d29-87a8-dddaa52b04c7.png align="center")
    
    ### Check Status
    
    ```plaintext
    git status  
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769628029506/f77f8c7b-3d8b-4435-9196-d3ea59463eab.png align="center")
    
    ### Save Changes
    
    ```plaintext
    git add file.txt  # Stage a file for commit
    git add .         # Stage all changes
    git commit -m "Fixed login bug"  # Save changes with a message
    ```
    
    ### View History
    
    ```plaintext
    git log           # See all commits (press 'q' to exit)
    git log --oneline # Compact view
    ```
    
    ### Work with Branches
    
    ```plaintext
    git branch        # List all branches
    git branch new-feature  # Create a new branch
    git checkout new-feature  # Switch to that branch
    ```
    
    ### Connect to Remote (GitHub/GitLab)
    
    ```plaintext
    git remote add origin https://github.com/your-repo.git
    git push -u origin main  # Upload your code
    ```
    
    ---
    
    ## 5\. Basic Developer Workflow (Step-by-Step)
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768599441616/62968b30-3c05-4fc1-bf13-7a1bced70f4a.png align="center")
    
    1. **Start a Project:**
        
        ```plaintext
        mkdir my-project
        cd my-project
        git init
        ```
        
    2. **Make Changes:**
        
        * Edit `index.html` and `style.css`.
            
    3. **Stage & Commit:**
        
        ```plaintext
        git add .
        git commit -m "Added homepage styling"
        ```
        
    4. **Create a Branch (for new features):**
        
        ```plaintext
        git branch add-login
        git checkout add-login
        ```
        
    5. **Push to Remote (GitHub):**
        
        ```plaintext
        git push origin add-login
        ```
        
    6. **Merge Changes (after review):**
        
        ```plaintext
        git checkout main
        git merge add-login
        ```
        
    
    ---
    
    ![Image of git-story: Create mp4 Video Animations of Your Git Commit History with 1 Command](https://initialcommit.com/img/initialcommit/git-story.png align="left")
    
    ## 6\. Pro Tips for Beginners
    
    * **Commit Often:** Small, frequent commits are easier to track.
        
    * **Write Good Messages:** Use clear, descriptive commit messages (e.g., "Fix login button alignment" instead of "fixed bug").
        
    * **Pull Before Push:** Always run `git pull` before pushing to avoid conflicts.
        
    * **Use** `.gitignore`: Tell Git to ignore files (like `node_modules/` or `.env`).
        
    
    ---
    
    ## 7\. Common Mistakes & Fixes
    
    | Mistake | Fix |
    | --- | --- |
    | Forgot to stage a file | `git add forgotten-file.txt` then commit again. |
    | Wrong commit message | `git commit --amend` to edit the last commit. |
    | Accidental commit to `main` | Create a new branch and reset `main`: `git branch temp; git reset --hard HEAD~1` |
    
    ---
    
    ## 8\. Summary: Git in One Paragraph
    
    Git is your **code’s safety net**. It lets you save versions, experiment in branches, and collaborate with others. Start with `init`, `add`, `commit`, and `push`—everything else builds on these. **Practice on small projects first!**
    
    ---
