# Git Restore and Git Reset

### **What Are Git Restore and Git Reset?**

Git is like a time machine for your code. It helps you save different versions of your project and go back to them if needed. Sometimes, you might make changes you don’t want to keep, or you might want to undo something you did earlier. That’s where `git restore` and `git reset` come in.

* `git restore`: This command helps you undo changes in your working directory (the files you’re currently working on). It’s like saying, “Oops, I don’t want these changes—let me go back to how it was.”
    
* `git reset`: This command is a bit more powerful. It helps you undo changes in your staging area (where you prepare files to be saved) or even go back to a previous commit (a saved version of your project).
    

Let’s look at how to use both commands step by step.

---

### **Step 1: Understand Your Git Workflow**

Before using these commands, it’s important to know the three main areas in Git:

1. **Working Directory**: This is where you make changes to your files.
    
2. **Staging Area**: This is where you add files you want to save in the next commit.
    
3. **Repository**: This is where your commits (saved versions) are stored.
    

---

### **Step 2: Using** `git restore`

Let’s say you made some changes to a file but realized you don’t want to keep them. Here’s how to undo those changes:

1. **Check the status**: Run `git status` to see which files have been changed.
    
    bash
    
    Copy
    
    ```bash
    git status
    ```
    
    This will show you the files you’ve modified.
    
2. **Undo changes in a specific file**: If you want to discard changes in one file, use:
    
    bash
    
    Copy
    
    ```bash
    git restore <file-name>
    ```
    
    For example, if you changed a file called `index.html`, run:
    
    bash
    
    Copy
    
    ```bash
    git restore index.html
    ```
    
    This will revert the file back to how it was in the last commit.
    
3. **Undo changes in all files**: If you want to discard all changes in your working directory, use:
    
    bash
    
    Copy
    
    ```bash
    git restore .
    ```
    
    This will undo all changes and bring your files back to the last saved state.
    

---

### **Step 3: Using** `git reset`

Now, let’s say you’ve added some files to the staging area (using `git add`) but want to unstage them. Or maybe you want to go back to a previous commit. Here’s how:

1. **Unstage a file**: If you added a file to the staging area but want to remove it, use:
    
    bash
    
    Copy
    
    ```bash
    git reset <file-name>
    ```
    
    For example, if you added `script.js` to the staging area, run:
    
    bash
    
    Copy
    
    ```bash
    git reset script.js
    ```
    
    This will remove the file from the staging area, but your changes will still be in your working directory.
    
2. **Go back to a previous commit**: If you want to undo a commit and go back to a previous version, use:
    
    bash
    
    Copy
    
    ```bash
    git reset --hard <commit-hash>
    ```
    
    The `commit-hash` is a unique code for each commit (you can find it by running `git log`). Be careful with this command—it will delete all changes after that commit.
    

---

### **Step 4: Practice and Experiment**

The best way to learn is by practicing! Create a test project or use a dummy file to try out these commands. Here’s a quick recap:

* Use `git restore` to undo changes in your working directory.
    
* Use `git reset` to unstage files or go back to a previous commit.
    

---

### **Step 5: Be Careful!**

Both `git restore` and `git reset` can undo changes, so make sure you really want to discard your work before using them. If you’re unsure, you can always create a new branch to experiment without affecting your main project.

---

## linkedin: [**https://www.linkedin.com/in/khushi-dubey-6036a2305**](https://www.linkedin.com/in/khushi-dubey-6036a2305)

# Thank You!
