Last Updated: January 3, 2026
git reset is a command that can alter the state of your working directory, the staging area, and the commit history itself.
Understanding how git reset operates will not only help you manage your code more effectively but will also empower you to navigate complex scenarios with confidence.
At its core, git reset is a way to move the current branch pointer to a different commit. Unlike git checkout, which allows navigation between branches and commits without changing the current branch, git reset alters the commit history of your branch.
When you invoke git reset, you have the ability to modify three key areas:
By understanding this command, you'll learn how to easily undo mistakes, whether they’re accidental commits or unwanted changes.
To fully grasp how git reset works, it's essential to understand the underlying Git object model. Git operates using three main components:
When you execute a git reset, you are essentially telling Git to change the HEAD pointer to a different commit and optionally modify the staging area and working directory as well.
Consider the following scenario where you have the following commit history:
If your HEAD is currently at commit D and you run git reset --hard B, you effectively move HEAD back to commit B. The commit graph would then look like this:
Commits C and D are no longer referenced by the master branch. Depending on the reset mode you choose, this operation can have different effects on your staging area and working directory.
The behavior of git reset is strongly influenced by the mode you choose: soft, mixed, or hard.
While we will delve deeper into each of these modes in the next chapter, it's crucial to have a basic understanding here.
git reset --soft <commit>): Moves HEAD to the specified commit, but leaves both the staging area and working directory as they are. This is useful for undoing a commit while keeping the changes staged for a new commit.git reset <commit> or git reset --mixed <commit>): The default mode if no flag is specified. It moves HEAD to the specified commit and resets the staging area to match it, leaving the working directory untouched. This is handy when you want to unstage changes but keep them in your working directory.git reset --hard <commit>): Moves HEAD back to the specified commit, resets the staging area, and also alters the working directory to match that commit. This is a destructive operation, as it discards all changes in the working directory that are not committed.Understanding when and how to use git reset can significantly enhance your workflow. Here are a few practical scenarios where git reset shines.
Imagine you’ve accidentally committed changes that you need to modify before finishing. You can use a soft reset to effectively undo the commit while keeping your changes staged:
After this, you can make your changes and recommit.
Suppose you’ve added files to the staging area, but you realize you want to modify them further before committing. You can use a mixed reset to unstage without losing your changes:
In a situation where you’ve made changes that you no longer want, a hard reset can be a lifesaver:
Be cautious with this operation, as it permanently removes any uncommitted changes.
While git reset is powerful, it can also lead to confusion and mistakes. Here are some common pitfalls:
It's easy to forget that a hard reset will discard changes in the working directory. Always double-check your current status with git status before executing a hard reset.
If you're uncertain about the outcome of a reset, consider creating a temporary branch. This allows you to experiment without altering your main branch:
If you’re working in a team and have already pushed changes to a shared repository, be wary of using git reset. This can cause issues for others who have based their work on your commits. Instead, consider using git revert in those scenarios.
If you find yourself in a situation where you've accidentally lost changes due to a reset, fear not. Git has mechanisms for recovery.
The reflog is a powerful feature that tracks where your HEAD has been. If you've reset and need to return to a previous state, you can use:
From the reflog, you can identify the commit hash and reset back to it:
This can save you from losing critical work.
Now that you understand the fundamentals of git reset, you are ready to explore its different modes: soft, mixed, and hard resets.
In the next chapter, we will look deeper into these modes, equipping you with the knowledge to apply them effectively in your workflows.