Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

Commit 5932b98

Browse files
authored
[Edit] Git: Reset (#7469)
1 parent 244932e commit 5932b98

1 file changed

Lines changed: 48 additions & 18 deletions

File tree

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
---
22
Title: 'Reset'
3-
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.'
3+
Description: 'Undoes changes by moving the HEAD pointer to a different commit.'
44
Subjects:
55
- 'Bash/Shell'
66
- 'Developer Tools'
77
Tags:
8+
- 'Bash/Shell'
89
- 'Git'
10+
- 'GitHub'
911
- 'Version Control'
1012
CatalogContent:
1113
- 'learn-the-command-line'
1214
- 'learn-git'
1315
---
1416

15-
The **`reset`** command is used to change the state of the Git repository or undo commits.
17+
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.
1618

17-
> **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.
19+
> **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.
1820
19-
## Syntax
21+
## Git Reset Syntax
2022

2123
```pseudo
22-
git reset <mode-option> <commit-reference>
24+
git reset <mode-options> <commit-reference>
2325
```
2426

25-
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.
26-
2727
### Mode Options
2828

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

3131
- Where the `HEAD` is pointing towards (usually done with just `git reset`).
3232
- Whether the staging area or index, reflects the commit the `HEAD` is now pointing towards.
@@ -38,43 +38,41 @@ More specifically, these modes include:
3838
- `--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).
3939
- `--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.
4040

41-
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.
42-
4341
### Referencing Commits
4442

45-
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`
43+
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`
4644

4745
`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.
4846

49-
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.
47+
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.
5048

51-
## Example
49+
## Example: Using Git Reset
5250

53-
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):
51+
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:
5452

5553
```shell
5654
On branch main
5755
nothing to commit, working tree clean
5856
```
5957

60-
The text above indicates the following:
58+
The text indicates that:
6159

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

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

6866
```shell
6967
git reset HEAD~1
7068
git reset --mixed HEAD~1
7169
```
7270

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

7573
- It will move the `HEAD` pointer back by one (`~1`) commit.
7674
- The staging area will be cleared of changes.
77-
- 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.
75+
- 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.
7876

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

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

8886
no changes added to commit (use "git add" and/or "git commit -a")
8987
```
88+
89+
## Git Reset vs. Git Revert
90+
91+
| Feature | `git reset` | `git revert` |
92+
| ------------------ | ------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------- |
93+
| **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. |
94+
| **History Impact** | Rewrites history (dangerous for shared branches). | Preserves history (safe for shared branches). |
95+
| **Data Loss Risk** | Possible (especially with `--hard`, which deletes changes). | No data loss — original commit remains in history. |
96+
| **Use Case** | Remove or modify commits before pushing (cleanup, rebase). | Safely undo changes in public/shared branches. |
97+
98+
## Frequently Asked Questions
99+
100+
### 1. What does `git reset` do?
101+
102+
`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:
103+
104+
- `--soft`: Moves `HEAD` only, keeps all changes staged.
105+
- `--mixed` (Default): Moves `HEAD` and resets the staging area, keeps changes in working directory.
106+
- `--hard`: Moves `HEAD` and discards both staged and working directory changes (data loss possible).
107+
108+
### 2. What is the difference between `git reset` and `git revert`?
109+
110+
- `git reset`: Rewrites commit history, moving `HEAD` to a different commit. Can remove commits entirely (dangerous for shared branches).
111+
- `git revert`: Creates a new commit which reverts the changes from a previous commit while keeping the history intact (safe for shared branches).
112+
113+
### 3. Is `git reset` a good idea?
114+
115+
It depends:
116+
117+
- **Good idea**: When cleaning up local commits before pushing (private branches).
118+
- **Risky**: On public/shared branches because it rewrites history and can cause conflicts for collaborators.
119+
- **Rule of Thumb**: Use `git reset` for local/private history cleanup; use `git revert` for undoing changes in shared/public branches.

0 commit comments

Comments
 (0)