The git reset command is used to move the current branch reference to another commit and can also change the staging area or working tree depending on the option used.
--soft, --mixed, or --hard
before running the command.
git reset is a command used to move the current branch reference to another commit and adjust the staging area and working tree according to the selected mode.
git reset
It is commonly used when you want to undo or reorganize local commits or remove changes from the staging area.
Git reset can be useful when you need to move the current branch backward in local history or change what is staged.
A common form of the command is:
git reset [options] [commit]
The result depends on the selected mode and the commit specified.
Git reset commonly moves the current branch reference to a specified commit.
git reset HEAD~1
Here, HEAD~1 means the parent of the current commit.
The expression HEAD~1 refers to one commit before the current HEAD.
HEAD
↓
Commit C
↓
Commit B
HEAD~1 = Commit B
Similarly, HEAD~2 refers to two commits before HEAD
along the first-parent ancestry.
The three commonly discussed reset modes are:
They differ mainly in what happens to the staging area and working tree.
The --soft mode moves the current branch to the specified commit but keeps the changes staged.
git reset --soft HEAD~1
The changes from the removed commit remain in the staging area.
Commit A → Commit B → Commit C
↑
HEAD
Run:
git reset --soft HEAD~1
The branch moves back to Commit B, while the changes represented by Commit C remain staged.
The --mixed mode moves the current branch and resets the staging area, while leaving working-tree changes intact.
git reset --mixed HEAD~1
This is the default mode when no mode is specified.
When no mode is specified, Git uses the mixed mode:
git reset HEAD~1
This moves the branch back and resets the staging area while keeping working-tree changes.
The --hard mode resets the current branch, staging area and working tree to the specified commit.
git reset --hard HEAD~1
--hard option can discard
uncommitted working-tree changes. Use it only when you are certain
that those changes are not needed.
| Mode | Branch | Staging Area | Working Tree |
|---|---|---|---|
--soft |
Reset | Kept | Kept |
--mixed |
Reset | Reset | Kept |
--hard |
Reset | Reset | Reset |
You can reset to a specific commit using its commit identifier:
git reset --mixed abc1234
The branch moves to the specified commit.
Use git log to find commit identifiers:
git log --oneline
For example:
8f32abc Update contact page
5d21ef4 Add contact page
1a72c90 Initial commit
git reset --mixed 5d21ef4
This moves the current branch to the specified commit and uses the mixed mode to reset the staging area while preserving working-tree changes.
Git reset can also be used to unstage a file:
git reset HEAD index.html
This removes the file from the staging area while keeping the working-tree changes.
A modern alternative for unstaging a file is:
git restore --staged index.html
This command makes the intention clearer by specifically describing a restore operation on the staging area.
Suppose the latest commit should be removed from the current branch. You can move the branch back one commit:
git reset --soft HEAD~1
The changes from the removed commit remain staged and can be committed again with a different message or after further editing.
git reset HEAD~1
Because mixed mode is the default, this moves the branch back one commit and unstages the changes while keeping the working files.
git reset --hard HEAD~1
This moves the branch back one commit and resets the index and working tree to match that commit.
--hard.
Reset is commonly used for changing local history. For example:
Commit A → Commit B → Commit C
After:
git reset --soft HEAD~1
the current branch points back to Commit B, while the changes from Commit C remain staged.
With the default mixed mode, reset changes the branch reference and staging area but leaves working-tree files unchanged.
git reset HEAD~1
You can then inspect the files and decide what to commit next.
After using reset, check the repository state:
git status
This helps you understand which changes are staged or unstaged.
Reset changes the local branch reference. If commits have already been shared with others through a remote repository, rewriting that history can create coordination problems.
For shared history, consider whether a command such as git revert is more appropriate.
git reset moves the current branch reference and can rewrite local history.
git revert creates a new commit that reverses the effect of an earlier commit.
git reset
git revert
The two commands solve different problems.
--hard without checking uncommitted changes.git status after reset.git reset HEAD index.html
git reset HEAD~1
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
git reset --mixed abc1234
git log --oneline
# View history
git log --oneline
# Check current state
git status
# Move back one commit but keep changes staged
git reset --soft HEAD~1
# Check state
git status
Always inspect your repository before and after a reset operation.
git init
touch index.html
git add index.html
git commit -m "Create homepage"
touch about.html
git add about.html
git commit -m "Create about page"
git log --oneline
git reset --soft HEAD~1
git status
The latest commit is removed from the current branch history while its changes remain staged.
The git reset command can move the current branch to another commit and control what happens to the staging area and working tree.
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
Remember:
Use reset carefully, especially when working with commits that have already been shared with others.
--soft keeps changes staged.--mixed resets the staging area but keeps working-tree changes.--hard resets the branch, staging area and working tree.git reset HEAD filename can unstage a file.HEAD~1 refers to the parent of the current commit.git log --oneline to find commit identifiers.git reset --hard.Question: Which git reset option resets the branch, staging area, and working tree to the specified commit?