# Git Guide: Understanding Essential Commands and Concepts

Git is a powerful version control system that helps developers manage their code efficiently. Below is a guide that walks through the most important Git commands, along with examples to clarify their usage.

---

### **1\.** `git add`: Adding Changes to Staging Area

The `git add` command adds your file changes to the **staging area** in Git. It prepares files for the next commit.

* **Example**:
    
    ```plaintext
    git add <file-name>  # Add specific file
    git add .            # Add all changes
    ```
    
    After modifying `index.html`, you can run `git add index.html` to stage the changes for the next commit.
    

---

### **2\.** `git commit`: Saving Changes to the Repository

`git commit` saves your staged changes to the local repository. Each commit represents a snapshot of your code at a particular point in time.

* **Example**:
    
    ```plaintext
    git commit -m "Added a new feature"
    ```
    
    This commits your staged changes with the message `"Added a new feature"`. It's best practice to use clear, concise commit messages.
    

---

### **3\.** `git branch`: Managing Branches

Branches allow you to work on different features or fixes independently without affecting the main codebase.

* **Example**:
    
    ```plaintext
    git branch feature-1  # Create a new branch
    git checkout feature-1  # Switch to the new branch
    ```
    
    The command `git branch feature-1` creates a new branch called `feature-1`.
    

---

### **4\.** `git push`: Pushing Changes to Remote Repository

Once your changes are committed locally, you can push them to a remote repository with `git push`.

* **Example**:
    
    ```plaintext
    git push origin feature-1
    ```
    
    This pushes the changes from your local `feature-1` branch to the `origin` remote repository.
    

---

### **5\.** `git pull`: Fetching and Merging Changes from Remote

`git pull` is used to fetch and merge changes from a remote repository into your current branch.

* **Example**:
    
    ```plaintext
    git pull origin main
    ```
    
    This command fetches and merges the latest changes from the `main` branch of the `origin` repository into your local branch.
    

---

### **6\.** `git remote`: Managing Remote Repositories

Remote repositories allow multiple developers to collaborate. You can add, view, or remove remotes with `git remote`.

* **Example**:
    
    ```plaintext
    git remote add origin https://github.com/user/repo.git
    git remote -v  # View remote URLs
    ```
    
    The `git remote add origin` command sets a remote repository URL that you can push and pull changes to.
    

---

### **7\.** `git config`: Configuring Git Settings

`git config` allows you to set configuration options, such as your name and email, which are used in commits.

* **Example**:
    
    ```plaintext
    git config --global user.name "John Doe"
    git config --global user.email "john@example.com"
    ```
    
    This sets your username and email globally for all repositories.
    

---

### **8\. Pull Request (PR): Collaboration with Code Review**

A Pull Request (PR) is a GitHub feature that allows for collaboration by requesting that someone reviews and approves your changes before merging them into the main branch.

* **Example**: In GitHub:
    
    1. Push your branch to the remote repository.
        
    2. Go to GitHub and create a new pull request from `feature-1` to `main`.
        
    3. Reviewers will provide feedback before the code is merged.
        

---

### **9\.** `git merge`: Merging Branches

`git merge` integrates changes from one branch into another. It preserves both branches' histories and creates a **merge commit**.

* **Example**:
    
    ```plaintext
    git checkout main
    git merge feature-1
    ```
    
    This merges the changes from the `feature-1` branch into the `main` branch.
    

---

### **10\.** `git rebase`: Rewriting Commit History

`git rebase` re-applies commits from your current branch on top of another branch, resulting in a **linear history**.

* **Example**:
    
    ```plaintext
    git checkout feature-1
    git rebase main
    ```
    
    This reapplies all commits from `feature-1` on top of `main`, making the commit history linear and avoiding a merge commit.
    

---

### **11\. Merge Conflict: Resolving Conflicts Between Branches**

Merge conflicts happen when Git cannot automatically merge changes. You will need to manually resolve the conflicts.

* **Example**: After attempting to merge:
    
    ```plaintext
    git merge feature-1
    ```
    
    If a conflict occurs, Git will notify you. You can resolve the conflicts in your code and then run:
    
    ```plaintext
    git add .
    git commit -m "Resolved merge conflict"
    ```
    

---

### **12\.** `git reset`: Undoing Commits

`git reset` moves the `HEAD` of the branch back to a previous state. It can modify the commit history and working directory.

* **Example**:
    
    ```plaintext
    git reset --soft HEAD~1  # Undo last commit, keep changes staged
    ```
    
    This undoes the last commit but keeps your changes in the staging area.
    

---

### **13\.** `git revert`: Undoing a Commit without Rewriting History

`git revert` creates a new commit that undoes the changes of a previous commit, without changing the commit history.

* **Example**:
    
    ```plaintext
    git revert <commit-hash>
    ```
    
    This creates a new commit that reverses the changes of the specified commit.
    

---

### **14\.** `git cherry-pick`: Applying Specific Commits from Another Branch

`git cherry-pick` allows you to pick a specific commit from another branch and apply it to your current branch.

* **Example**:
    
    ```plaintext
    git cherry-pick <commit-hash>
    ```
    
    This applies the changes from a specific commit in one branch to your current branch.
    

---

### **15\.** `git log`: Viewing Commit History

The `git log` command displays the commit history of your repository. It helps you see the order of commits and their details.

* **Example**:
    
    ```plaintext
    git log
    git log --oneline  # Compact view
    ```
    
    This shows the commit history. The `--oneline` option provides a summarized view, showing only the commit hash and message.
    

---

### **16\.** `git stash`: Temporarily Saving Changes

`git stash` allows you to temporarily save your changes without committing them, so you can work on something else without losing your progress.

* **Example**:
    
    ```plaintext
    git stash  # Save changes to stash
    git stash apply  # Reapply the changes from stash
    git stash drop  # Remove stash after applying
    ```
    
    After running `git stash`, your changes will be removed from the working directory but saved in a "stash". You can later reapply those changes with `git stash apply`.
