Using ‘git reset –hard’ to undo local changes (with examples)

Updated: January 27, 2024 By: Guest Contributor Post a comment

Overview

If you’re involved with software development or you work within a team managing code changes, you’ll likely encounter the need to undo changes to your Git repository. Understanding how to revert a repository to a previous state is a fundamental skill that can help avoid mistakes and maintain project integrity. In this post, we’ll explore the usage of git reset --hard, a command that allows you to rollback changes and bring your project back to a desired state.

Before venturing further into examples, let’s ensure we understand what git reset --hard does. This command is used to ‘reset’ the current HEAD of your local repository to a specific state. All changes you’ve made since that commit will be discarded completely. It’s a powerful tool that should be used with caution, as it can lead to a loss of work.

Basic Git Reset

$ git reset --hard HEAD^

This command rolls back the repository to the state it was just before the latest commit. In terms of changes, if you committed something you immediately know is wrong, this will remove that commit altering your working directory and staging index.

$ git log --oneline
c5d2c21 (HEAD) Add navbar functionality
ae3f6e5 Update CSS styles
dd3daf5 Initial commit

$ git reset --hard HEAD^
HEAD is now at ae3f6e5 Update CSS styles

Undoing Uncommitted Changes

If you have uncommitted changes that you want to discard, git reset --hard can be used without specifying a commit to return to the current HEAD.

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

	modified:   index.html

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

	modified:   styles.css

$ git reset --hard
HEAD is now at ae3f6e5 Update CSS styles
$ git status
On branch master
nothing to commit, working tree clean

Resetting to an Older Commit

To reset your repository to the state of a specific older commit, include the commit’s hash like so:

$ git reset --hard dd3daf5

All changes after commit dd3daf5 will be undone. This is great if a certain commit introduced bugs or changes that are no longer desired.

$ git log --oneline
$ git reset --hard dd3daf5
HEAD is now at dd3daf5 Initial commit

Resetting in a Remote Context

Beware that git reset --hard affects only your local repository. If you’ve already pushed commits to a remote repository, you will need to force push the rollback which can disrupt your team. A safer alternative might be to revert those commits instead of hard resetting.

$ git push origin master --force

It’s recommended to communicate with your team before performing such an action.

Advanced Scenarios

In some cases, you might want to preserve your local changes, but reset your repository’s index and working directory. For this purpose, use git reset --soft followed by git stash.

$ git reset --soft HEAD^
$ git stash
Saved working directory and index state WIP on master: ae3f6e5 Update CSS styles
HEAD is now at dd3daf5 Initial commit

Now your changes are stashed and you can apply them later if needed.

Conclusion

Using git reset --hard empowers developers to resolve mistakes by reverting a repository to a previous state. Always use this command with the knowledge that it will irreversibly discard changes. When in doubt, consider other options like git revert or git stash which are less destructive and offer a safety net.