Methods of payment Abuse

What Does git reset Do — And Why You Might Need It

05.06.2025, 17:55

When working with code, things don’t always go according to plan. Maybe you committed the wrong file, made changes in the wrong branch, or just want to undo your last few steps. In cases like these, Git gives you a powerful tool — git reset. This command helps you roll back changes, clean up unwanted commits, or prepare your code for a new commit. It works with precision and flexibility: you can remove only the last commit and keep all your files as they are — or wipe the slate completely clean.

How git reset Works — In Simple Terms

Git tracks your work in three areas:

→ Working directory — the actual files in your project that you’re editing
→ Index (staging area) — a place to collect changes before committing
→ Repository — the saved history of your project in the form of commits

The git reset command can manage all three areas. Depending on the mode you choose (--soft, --mixed, or --hard), it decides how much to roll back — just the commit, the index too, or everything including the working files.

When to Use git reset

This command is helpful if:

→ You accidentally committed the wrong file
→ You want to improve a commit before publishing
→ You want to try a different approach by stepping back a few commits
→ You pushed unnecessary changes to your local branch (but haven’t pushed them to the remote yet)

Git generally avoids rewriting history, especially if commits have been shared. But for local work, git reset is incredibly useful.

The Three Modes of git reset

--soft

This mode removes the commit but keeps changes in the index. In other words, your code is still staged and ready to recommit.

Example:

git add file.py  
git commit -m "bad commit"  
git reset --soft HEAD~1

Now you can fix the issue and make a clean new commit — no fuss.

--mixed (default)

This mode undoes the commit and clears the staging area, but your files stay unchanged in the working directory.
Why use it: You changed your mind about what to commit and want to choose carefully again.

Example:

git reset HEAD~1

Now you can re-add files using git add, but with more intention.

--hard

The most extreme option — it deletes the commit, clears the index, and discards changes from the working directory.

Warning: This action is irreversible — unless you recover using git reflog.

Example:

git reset --hard HEAD~1

Use this only when you’re absolutely sure you don’t need those changes anymore.

When to Use git revert Instead

If you've already pushed a commit to a shared repository (like GitHub), avoid using git reset. It rewrites history and may cause sync issues for others. In such cases, prefer git revert. It doesn’t delete the commit — instead, it creates a new commit that undoes the changes, keeping the history clean and linear.

How to Remove a File from the Staging Area

If you don’t want to reset a commit, but just want to unstage a file, use:

git rm --cached filename

This removes the file from the index but keeps it on disk — useful if you added something to the commit by mistake.

Deleting Local Commits — With or Without Keeping Files

If you made several local commits and want to undo them:

→ To keep the file changes:

git reset --soft HEAD~N

→ To discard everything:

git reset --hard HEAD~N

Where N is the number of commits you want to remove.

Changed Your Mind? Use git reflog

git reset is powerful — but risky. Luckily, Git has your back. You can use git reflog to view the history of your HEAD movements and recover lost commits:

git reflog
git reset --hard <desired-hash>

Conclusion

git reset is the surgeon of Git commands. It lets you undo mistakes with surgical precision — whether you want to clean up a messy history or prep for a better commit. Just use it wisely, especially if you're working with shared history. And if something goes wrong, don’t panic — git reflog can often save the day.