Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 48 additions & 18 deletions content/git/concepts/reset/reset.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
---
Title: 'Reset'
Description: 'The git reset command is used to set the HEAD pointer back to a previous commit and optionally undo staged changes and the working tree.'
Description: 'Undoes changes by moving the HEAD pointer to a different commit.'
Subjects:
- 'Bash/Shell'
- 'Developer Tools'
Tags:
- 'Bash/Shell'
- 'Git'
- 'GitHub'
- 'Version Control'
CatalogContent:
- 'learn-the-command-line'
- 'learn-git'
---

The **`reset`** command is used to change the state of the Git repository or undo commits.
The **`git reset`** command undoes changes by moving the HEAD pointer (which indicates the current commit) to a different commit in the repository's history. It can also affect the staging area (index) and the working directory.

> **Note:** This command is ideal for undoing uncommitted changes made in a private, local repository. For undoing changes in a public, remote repository, the `revert` command is recommended.
> **Note:** This command is ideal for undoing uncommitted changes made in a private, local repository. For undoing changes in a public, remote repository, the `git revert` command is recommended.

## Syntax
## Git Reset Syntax

```pseudo
git reset <mode-option> <commit-reference>
git reset <mode-options> <commit-reference>
```

This is run in the [terminal](https://www.codecademy.com/resources/docs/command-line/terminal). The `<mode-options>` and `<commit-reference>` are discussed in more detail below.

### Mode Options

The `<mode-options>` refer to how far `reset` will go when rolling back changes to a previous commit, including:
The `<mode-options>` flag refers to how far `git reset` will go when rolling back changes to a previous commit, including:

- Where the `HEAD` is pointing towards (usually done with just `git reset`).
- Whether the staging area or index, reflects the commit the `HEAD` is now pointing towards.
Expand All @@ -38,43 +38,41 @@ More specifically, these modes include:
- `--mixed`: In addition to moving the `HEAD` pointer to an earlier commit, the staging area is cleared to reflect the changes made in that commit (this runs by default).
- `--hard`: This goes one step further and resets the working tree to reflect the previous commit reflected in the staging area and the `HEAD` pointer.

If, for example, an error was made in a text file,`example.txt`, and the changes were accidentally [added](https://www.codecademy.com/resources/docs/git/add) and [committed](https://www.codecademy.com/resources/docs/git/commit), `git reset` can be used to go back to the state before that commit was made.

### Referencing Commits

The `commit-reference` refers to a commit's unique hash, or save point, that was generated after creation. This hash is a long string that is a mix of characters and numbers that is usually represented by a shorter version: `05df67f9066c8ddd95c8d7bb2137acfb8b18e167` -> `05df67f`
The `<commit-reference>` flag refers to a commit's unique hash, or save point, that was generated after creation. This hash is a long string that is a mix of characters and numbers that is usually represented by a shorter version: `05df67f9066c8ddd95c8d7bb2137acfb8b18e167` -> `05df67f`

`git reset` can be used with either the commit hash or with the `HEAD` keyword, which refers to the commit being viewed on the currently checked-out branch.

Alternatively, a filename can be used in place of the `commit-reference` to undo a `git add` for a file that wasn't meant to be staged for commit.
Alternatively, a filename can be used in place of `<commit-reference>` to undo a `git add` for a file that wasn't meant to be staged for commit.

## Example
## Example: Using Git Reset

This is what the terminal would look like after creating a commit by accident on the `main` branch and running [a `git status` check](https://www.codecademy.com/resources/docs/git/status):
This is what the terminal would look like after creating a commit by accident on the `main` branch and running a [`git status`](https://www.codecademy.com/resources/docs/git/status) check:

```shell
On branch main
nothing to commit, working tree clean
```

The text above indicates the following:
The text indicates that:

- The `main` branch is up to date, with the `HEAD` pointing towards the most recent commit.
- There is nothing to commit in the staging area.
- New changes haven't been made yet in the working tree, hence why it is "clean".

To set the `HEAD` back by one commit as well as clear the staging area, one of the following commands can be run:
To set the `HEAD` back by one commit as well as clear the staging area, one of these commands can be run:

```shell
git reset HEAD~1
git reset --mixed HEAD~1
```

Since the `--mixed` mode runs by default, both of the commands are identical in function. This will do the following:
Since the `--mixed` mode runs by default, both of the commands are identical in function. Upon running one of these:

- It will move the `HEAD` pointer back by one (`~1`) commit.
- The staging area will be cleared of changes.
- The overall state of the `main` branch is set to before changes in `example.txt` were added to the staging area for the previous commit.
- The overall state of the `main` branch is set to the state before changes in `example.txt` were added to the staging area for the previous commit.

If `git status` is run once more, this should appear on the terminal:

Expand All @@ -87,3 +85,35 @@ Changes not staged for commit:

no changes added to commit (use "git add" and/or "git commit -a")
```

## Git Reset vs. Git Revert

| Feature | `git reset` | `git revert` |
| ------------------ | ------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------- |
| **Purpose** | Moves the current branch’s `HEAD` to a specific commit, changing the commit history. | Creates a new commit which reverts the changes from a specific commit without altering history. |
| **History Impact** | Rewrites history (dangerous for shared branches). | Preserves history (safe for shared branches). |
| **Data Loss Risk** | Possible (especially with `--hard`, which deletes changes). | No data loss — original commit remains in history. |
| **Use Case** | Remove or modify commits before pushing (cleanup, rebase). | Safely undo changes in public/shared branches. |

## Frequently Asked Questions

### 1. What does `git reset` do?

`git reset` moves the current branch's `HEAD` to a specified commit. Depending on the mode (`--soft`, `--mixed`, or `--hard`), it can also change the staging area and working directory:

- `--soft`: Moves `HEAD` only, keeps all changes staged.
- `--mixed` (Default): Moves `HEAD` and resets the staging area, keeps changes in working directory.
- `--hard`: Moves `HEAD` and discards both staged and working directory changes (data loss possible).

### 2. What is the difference between `git reset` and `git revert`?

- `git reset`: Rewrites commit history, moving `HEAD` to a different commit. Can remove commits entirely (dangerous for shared branches).
- `git revert`: Creates a new commit which reverts the changes from a previous commit while keeping the history intact (safe for shared branches).

### 3. Is `git reset` a good idea?

It depends:

- **Good idea**: When cleaning up local commits before pushing (private branches).
- **Risky**: On public/shared branches because it rewrites history and can cause conflicts for collaborators.
- **Rule of Thumb**: Use `git reset` for local/private history cleanup; use `git revert` for undoing changes in shared/public branches.