When working with Git, there are times you need to back up a step, undo a commit, or reorganize your project history. That’s where reset commands come in. Among them, the two most common are git reset soft and git reset hard.
Both reset your repository to a previous commit, but the results are very different. Misusing one can wipe away hours of work, while the other can give you flexibility to clean up history without losing changes.
This guide focuses on git reset soft — breaking it down step by step so you’ll know exactly when and how to use it. We’ll also contrast it with git reset hard
, explore what happens with untracked files, and walk through real scenarios where soft reset command shines.
Step 1: Understanding What Git Reset Does
The git reset
command moves the HEAD (your current commit pointer) to a different commit. Depending on the reset mode, Git will also decide what to do with:
- The staging index (what’s ready to be committed).
- The working directory (the files you see on disk).
There are three main reset modes:
- soft → keeps changes staged.
- mixed → unstages changes but leaves them in your working directory.
- hard → discards both staged and working directory changes.
For example, if you’re learning about deleting branches in Git, the same principle applies: you’re moving or cleaning pointers. Reset is just a more powerful version of history manipulation.
Step 2: Using Git Reset Hard (for comparison)
Before diving into soft resets, it’s useful to look at what happens when you run a hard reset:
git reset --hard <commit-id>
This command:
- Moves HEAD back to the given commit.
- Clears the staging index.
- Overwrites your working directory to match that commit.
Example
Imagine you made five commits. You realize commits 4 and 5 broke your project. Running:
git reset --hard HEAD~2
takes you back to commit 3. Files introduced in commits 4 and 5 vanish from disk. It’s like they never happened.
⚠️ The danger: once you do this, unless you recover with git reflog
, those changes are gone. That’s why developers treat git reset hard
as a “last resort” command.

Step 3: Using Git Reset Soft
Now let’s turn to soft reset in Git, the safer alternative:
git reset --soft <commit-id>
When you run this command, Git:
- Moves HEAD to the target commit.
- Leaves your working directory unchanged.
- Keeps all your changes staged in the index.
In other words, the commit history is rewound, but your actual code is still there, ready to recommit.
Example
Continuing with the five-commit scenario:
git reset --soft HEAD~2
Rewinds history to commit 3. But the changes from commits 4 and 5 remain staged. You can:
- Combine them into one new commit.
- Break them into smaller, cleaner commits.
- Or adjust the code before recommitting.
This flexibility is why git reset soft is a favorite for developers who want to polish their history before pushing to a shared repository.
Step 4: Practical Scenario – Cleaning Messy Commits
Let’s say you’re building a feature and accidentally commit unrelated changes together:
- Commit 1 → Added login form.
- Commit 2 → Fixed a typo and added a new API integration in the same commit.
Now commit 2 is messy. Instead of containing one logical change, it contains two.
By running:
git reset --soft HEAD~1
You rewind to before commit 2. Your changes remain staged. Now you can:
git reset HEAD
git add typo-fix.js
git commit -m "Fix typo"
git add api.js
git commit -m "Add API integration"
Result? Two clean commits instead of one cluttered commit.
If you’re interested in managing commits better, you may also want to check out how to move commits to another branch in Git.
Step 5: What Happens with Untracked Files?
One common question: does soft reset command delete untracked files?
The answer: no.
Reset commands only affect tracked files (files already added to Git at some point). If you have untracked files lying around, they won’t be touched.
For example, if you created a notes.txt
file and never staged it, running git reset soft
will not delete it.
To clean up untracked files, you’d use:
git clean -f
We’ve explained this in detail in our guide on the Git clean command.
Step 6: Choosing Between Hard and Soft Reset
Here’s how to decide:
- Use soft reset when:
- You want to restructure commits.
- You accidentally committed too much at once.
- You want to combine several commits into one.
- Use a hard reset when:
- You want to throw away all changes and start fresh.
- You need to restore your working directory to a known stable commit.
- You’re certain you don’t need the discarded work.
A good rule of thumb: if you’re unsure, use a soft reset first. You can always clean up further, but once a hard reset is done, recovery can be painful.
Step 7: Best Practices for Safe Resets
Reset is powerful, but it can be dangerous if misused. Follow these tips:
- Don’t reset shared branches
Avoidgit reset
on a branch others are working on. It rewrites history, causing conflicts. - Check the commit ID carefully
Usegit log --oneline
before resetting. Resetting to the wrong commit can undo the wrong work. - Use Git Stash for safety
Unsure about losing work? Rungit stash
first. You can learn more about stash management in our detailed article on the Git stash list. - Learn alternatives
In collaborative workflows,git revert
it may be safer. Unlike reset, revert doesn’t rewrite history but instead creates a new commit undoing the changes.
For deeper dives, see Git’s official documentation on reset or Atlassian’s guide to undoing changes.
Conclusion
Both git reset soft and git reset hard are essential Git tools, but they serve very different purposes:
git reset hard
wipes out changes to match a specific commit.git reset soft
preserves your code while rolling back the commit history.
For everyday cleanup and commit restructuring, soft reset command is the safer, more flexible option. Reserve git reset hard
for moments when you truly want a clean slate.
Mastering the difference ensures you can rewrite history with confidence, keep your repository clean, and avoid the dreaded “I just lost all my code” moment.
Leave a Reply