# Undoing Changes with Git - Reverting and Resetting

**Undoing Changes with Git - Reverting and Resetting**

In Git, undoing changes is an essential aspect of version control. Sometimes, we make mistakes or need to revert certain changes in our codebase. Git provides two primary methods for undoing changes: **reverting** and **resetting**.&#x20;

Each method has its use cases, and understanding when to use them is crucial for managing your project's history effectively.

**Reverting**

When you want to undo the changes made in a specific commit, without altering the commit history, you can use the `git revert` command. This command creates a new commit that effectively undoes the changes introduced by the target commit. This means that the original commit remains in the history, but its changes are negated.

**Resetting**

On the other hand, `git reset` is a more powerful and dangerous command that moves the branch pointer to a different commit, effectively erasing some commits from the commit history. There are three different modes for `git reset`: soft, mixed, and hard, each having different effects on the staged area and working directory.

***

### **How to use the Git revert command to undo specific commits**

Let's assume we have a Git repository with several commits and we want to undo the changes introduced by a specific commit.

Step 1: **Find the commit hash you want to revert**&#x20;

You can use `git log` to see the commit history.

```bash
git log
```

Step 2: **Use the `git revert` command followed by the commit hash you want to revert.**

```bash
git revert <commit-hash>
```

For example, if the commit hash is `abcde123`, the command would be:

```bash
git revert abcde123
```

Git will create a new commit that undoes the changes introduced by the commit `abcde123`, leaving the original commit intact.

***

### **Exploring the Git reset command and its different modes for undoing changes**

Let's delve into the three modes of `git reset`:

**Soft Reset**: This mode moves the branch pointer to a specific commit without changing the staged area or working directory. It effectively "uncommits" the changes, leaving the changes in the files. To perform a soft reset, use the following command:

```bash
git reset --soft <commit-hash>
```

**Mixed Reset**: This mode is the default behaviouhr of `git reset` when no mode is specified. It moves the branch pointer to a specific commit and resets the staged area, but it keeps the changes in the working directory. To perform a mixed reset, use the following command:

```bash
git reset <commit-hash>
```

**Hard Reset**: This mode is the most powerful and potentially dangerous. It moves the branch pointer to a specific commit, resets the staged area, and erases all changes in the working directory. Be cautious when using this mode, as you can lose unsaved changes. To perform a hard reset, use the following command:

```bash
git reset --hard <commit-hash>
```

Remember to replace `<commit-hash>` with the actual hash of the commit you want to reset.

**Caution**: Be cautious when using `git reset` with `--hard`, as it irreversibly removes changes. Make sure you have saved any important changes elsewhere before executing a hard reset.

{% hint style="info" %}
These powerful commands are essential tools in your version control toolkit. Use them wisely to maintain a clean and organized commit history while effectively managing your project's codebase.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitdeveloperguide.solomonmarvel.com/git-troubleshooting/undoing-changes-with-git-reverting-and-resetting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
