How to revert to a previous commit in Git?

Modified on Tue, 28 Mar, 2023 at 3:56 PM

Problem Manifestation: I accidentally committed the wrong changes and pushed them to the remote repository. How can I revert to a previous commit?


Understand the problem: Reverting to a previous commit in Git means going back to an earlier version of your code. This can be useful if you accidentally made a mistake or introduced a bug in your code. When you revert to a previous commit, Git creates a new commit that undoes the changes made in the previous commit.


Methods for solving the problem:


  • Method 1: Reverting a commit


  1. First, make sure you are in the correct Git repository by navigating to the project directory in your terminal or command prompt.

  2. Use the git log command to find the commit ID of the commit you want to revert to. The commit ID is a unique identifier for each commit.

  3. Use the git revert <commit ID> command to revert the commit. This will create a new commit that undoes the changes made in the previous commit.

  4. Use the git push command to push the new commit to the remote repository.


Code Example:


$ git log

commit 283f31b0e0dbd8d238ac9e10a0f685e68c8cc8a1 (HEAD -> master)

Author: John Doe <johndoe@example.com>

Date: Mon Aug 30 13:35:00 2021 -0400


Fix typo in README.md


commit 9d475e69364e25662fb1d88c78b73f23809c29a3

Author: John Doe <johndoe@example.com>

Date: Fri Aug 27 14:05:00 2021 -0400


Initial commit


$ git revert 283f31b0e0dbd8d238ac9e10a0f685e68c8cc8a1

[master 8dbb050] Revert "Fix typo in README.md"

1 file changed, 1 deletion(-)



  • Method 2: Resetting to a previous commit


  1. First, make sure you are in the correct Git repository by navigating to the project directory in your terminal or command prompt.

  2. Use the git log command to find the commit ID of the commit you want to revert to. The commit ID is a unique identifier for each commit.

  3. Use the git reset --hard <commit ID> command to reset your local repository to the specified commit. This will remove all changes made after that commit.

  4. Use the git push --force command to force push the changes to the remote repository. This is necessary because you have rewritten the history of your Git repository.


Code Example:


$ git log

commit 283f31b0e0dbd8d238ac9e10a0f685e68c8cc8a1 (HEAD -> master)

Author: John Doe <johndoe@example.com>

Date: Mon Aug 30 13:35:00 2021 -0400


Fix typo in README.md


commit 9d475e69364e25662fb1d88c78b73f23809c29a3

Author: John Doe <johndoe@example.com>

Date: Fri Aug 27 14:05:00 2021 -0400

    

Initial commit


$ git reset --hard 9d475e69364e25662fb1d88c78b73f23809c29a3

HEAD is now at 9d475e6 Initial commit


$ git push --force 


Extra Information:


It's important to note that reverting to a previous commit is different from resetting to a previous commit. When you revert to a previous commit, Git creates a new commit that undoes the changes made in the commit you are reverting to. When you reset to a previous commit, Git removes all commits that were made after the commit you are resetting to. Be careful when using the reset command, as it can permanently delete commits that you may want to keep.


To revert to a previous commit, you must first get the commit ID. So how do you get the commit ID! To do that, run the command -


git log --oneline

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article