How to Revert Local Changes in Git Repository to a Previous State?

Git is the foundation upon which all version control systems are built and it helps developers manage their changes. However, people always make mistakes and you may want to discard your local coding changes. This article will explain different approaches to reverting your local hacks back to your git classifier and restore some experts before then.

Table of Content

  • What are Local Changes?
  • Approach 1: Using ‘ git checkout ‘
  • Approach 2: Using ‘ git reset ‘
  • Choosing the Right Approach

What are Local Changes?

Local changes are changes you have made to your working directory, and they have not yet been committed to your Git repository. This implies that the changes are currently on your own computer and are not yet integrated into the project’s history.

Approach 1: Using ‘ git checkout ‘

This method is suitable when you only want to discard uncommitted changes and don’t need to revert to a specific commit.

  • Identify Files :

If you want to discard changes only for specific files, use the following command, replacing <filename> with the actual filename:

    git checkout <filename>

This command is used to update your working directory based on a specific reference (usually a branch or commit).

  • for example :

using git checkout

  • Discard All Changes:

To discard unstaged changes for the entire working directory, use this command:

git checkout  .

‘ . ‘ : Represents the current directory, indicating all files should be reverted.

Approach 2: Using ‘ git reset ‘

This approach allows you to revert to a specific commit and discard both staged (added to the staging area) and unstaged changes. It’s more powerful but requires caution as it rewrites history.

  • Identify Commit (Optional):

Use git log to view your commit history and identify the commit you want to revert to.

git log
  • Reset to Specific Commit (Hard Reset):

To completely discard all changes made after the chosen commit (including staged and unstaged), use the following command, replacing <commit-hash> with the actual commit hash:

    git reset --hard <commit-hash>

–hard: Discards all changes (staged and unstaged) after the specified commit.

  • Reset with Index (Mixed Reset)

This option discards unstaged changes but keeps staged changes. Use the following command, replacing <commit-hash> with the actual commit hash:

git reset --mixed <commit-hash>

–mixed: Discards unstaged changes but keeps staged changes.

Choosing the Right Approach

  • Discard unstaged changes for specific files:
 git checkout <filename>
  • Discard all unstaged changes:
git checkout .
  • Revert to a specific commit and discard all changes:
git reset --hard <commit-hash>
  • Revert to a specific commit and keep staged changes:
git reset --mixed <commit-hash>

Note : whenever employing git reset and particularly a hard reset, make sure that you have backed up any local changes in order to avoid unforeseen outcomes.

If you want to undo local changes in your Git repository and go back to a previous version, should try these methods. You should select the one that best suits


Contact Us