# Undoing commits

### ⭕ Undoing commits

&#x20;   ▪ git reset --hard and --soft

&#x20;   ▪ git revert

### 🟢 GIT RESET

Reset performs different actions depending on the arguments:

* With a path
* Without a path

By default, git performs `git reset --mixed`.

`git checkout` will move the HEAD but the branch stays where it was. `git reset` will move the HEAD and the branch reference, meaning the branch is now modified.

For commits: moves the HEAD pointer, and optionally modifies files.\
For file paths: does not move HEAD pointer, modifies files.

There are three options for `git reset`:

1️⃣ **GIT RESET --soft**

&#x20;   ▪ **moves HEAD**

`git reset --soft HEAD~` now HEAD points to the previous commit (because `^` indicates parent of the current commit).

Before:

![](https://res.cloudinary.com/practicaldev/image/fetch/s--ChLwwGQB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99ztp3pzo53qo3shfc31.png)

After:<br>

![](https://res.cloudinary.com/practicaldev/image/fetch/s--sEjtiK3t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rmnpnyhkz4q1hned4z53.png)

2️⃣ **GIT RESET --mixed (the default)**

&#x20;   ▪ **move HEAD, copy files to stage**

Before:<br>

![](https://res.cloudinary.com/practicaldev/image/fetch/s--R7zHN-HC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a9xob3c40qzs0s5molow.png)

After:<br>

![](https://res.cloudinary.com/practicaldev/image/fetch/s--2FFUKk6L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdbjppul3wib099f6gk2.png)

3️⃣ **GIT RESET --hard**

&#x20;   ▪ **move HEAD, copy files to stage & working! (destructive operation because you lose files untracked in the working area)**

Before:<br>

![](https://res.cloudinary.com/practicaldev/image/fetch/s--aaDxULax--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o2ueepgdikvl5xeam0he.png)

After:

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FcF0gh16DVy5q7j4ysOik%2F62rjgpys95afop3w4xti.png?alt=media\&token=81d2b2ca-3282-4d42-a390-8f900fc9d72a)

`git reset --hard` cleans staging and working trees.

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FRiDDTGAS3mOL2yU6GUqr%2Fimage.png?alt=media\&token=606de5b0-3a30-4fbe-9fee-a8b329b892b8)

```bash
git reset --hard HEAD~1 #
```

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FgwSGVBxCT9LqUOMEuXaK%2Fimage.png?alt=media\&token=9c8c1704-773c-477a-bb32-f2d45a80f997)

### 🟢 GIT REVERT

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FopXD5wmFYrLqUVNJ7pNp%2Fimage.png?alt=media\&token=e5de1b11-86ee-4c88-9cf6-a5a294d2860e)
