DEV Community

Cover image for Undoing Local and Remote Changes with git revert
OpenReplay Tech Blog
OpenReplay Tech Blog

Posted on

Undoing Local and Remote Changes with git revert


When developing, mistakes are around the corner, and you may need to undo a commit. Here is where the git revert command comes in! This article will teach you how to revert both local and remote commits.


Session Replay for Developers

Uncover frustrations, understand bugs and fix slowdowns like never before with OpenReplay — an open-source session replay suite for developers. It can be self-hosted in minutes, giving you complete control over your customer data.

OpenReplay

Happy debugging! Try using OpenReplay today.


git revert is the command to revert some existing commits. To "revert" a commit means to record a new commit that undoes the changes introduced by one or more previous commits. (To delete a branch, follow our guide instead.)

The syntax of the git revert command is as follows, where <commit-hash> is the hash of the commit you want to revert:

git revert <commit-hash> 
Enter fullscreen mode Exit fullscreen mode

Note: If you specify the hash of an old commit, git revert will only revert that specific commit, not the subsequent ones.

When you perform a revert commit operation, Git automatically creates a new commit with the following name format:

Revert "<original-commit-message>"

This reverts commit "<commit-hash>"
Enter fullscreen mode Exit fullscreen mode

To prevent git revert from automatically committing the changes, use the -n or --no-commit option:

git revert -n <commit-hash>
Enter fullscreen mode Exit fullscreen mode

How to Revert a Commit

Let's see how to use git revert to undo a commit, both locally and remotely.

Revert a Local Commit

The procedure to revert a local commit involves two steps:

  1. Use the git log command to identify the hash of the commit you want to revert:
git log
Enter fullscreen mode Exit fullscreen mode

This returns a list of recent commits along with their commit hashes, as below:

commit 7c0663eba284fbaba9d238c86fa58370ec5fef04 (HEAD -> master)
Author: Antonello Zanini antonello@writech.run
Date: Fri Nov 15 12:05:56 2024 +0100

 comments added

commit 826a4cd9aee30602811b5970c9bff9e345aa26f3
Author: Antonello Zanini antonello@writech.run
Date: Fri Nov 15 12:05:28 2024 +0100

 'Hello, World!' logic added

commit 184c11bd2d20c684ece47dc732c2ac65625868ca
Author: Antonello Zanini antonello@writech.run
Date: Fri Nov 15 12:04:16 2024 +0100

 First commit
Enter fullscreen mode Exit fullscreen mode
  1. Execute git revert to undo the changes of the chosen commit:
git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Replace <commit-hash> with the hash of the commit you want to revert.

For example:

git revert 7c0663eba284fbaba9d238c86fa58370ec5fef04
Enter fullscreen mode Exit fullscreen mode

Git will open the following file in your default text editor (in this case, Notepad++):

If you close the text editor without making any changes, Git will create a commit using the default message format. The output will be:

[master b853916] Revert "comments added"
 1 file changed, 2 deletions(-)
Enter fullscreen mode Exit fullscreen mode

Verify that the revert commit was created with git log:

commit b8539162ff05c1783a58a52219781cef60f1c31f (HEAD -> master)
Author: Antonello Zanini <antonello@writech.run>
Date:   Fri Nov 15 12:06:45 2024 +0100

 Revert "comments added"

 This reverts commit 7c0663eba284fbaba9d238c86fa58370ec5fef04.

commit 7c0663eba284fbaba9d238c86fa58370ec5fef04
Author: Antonello Zanini <antonello@writech.run>
Date:   Fri Nov 15 12:05:56 2024 +0100

 comments added

# omitted for brevity...
Enter fullscreen mode Exit fullscreen mode

The first commit in the list is the one created by git revert.

Otherwise, you can customize the revert commit message by editing the default message in the editor:

Save the file, close the editor, and a revert commit with the custom message will be created:

commit 8cfe847922f7290c0f03d51108ac2b692298fda2 (HEAD -> master)
Author: Antonello Zanini <antonello@writech.run>
Date:   Fri Nov 15 12:14:13 2024 +0100

 "comments added" comment reverted

commit 7c0663eba284fbaba9d238c86fa58370ec5fef04
Author: Antonello Zanini <antonello@writech.run>
Date:   Fri Nov 15 12:05:56 2024 +0100

 comments added

# omitted for brevity...
Enter fullscreen mode Exit fullscreen mode

Note the commit message of the first commit—which is the revert commit—from git log.

Revert a Remote Commit

The procedure to revert a remote commit involves a few additional steps compared to reverting a local commit:

  1. Use git checkout to switch to the local branch corresponding to the remote branch you want to revert a commit on:
git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

Replace <branch-name> with the name of the local branch matching the remote branch.

  1. Utilize git fetch to verify that your local repository is up-to-date with the remote origin:
git fetch origin
Enter fullscreen mode Exit fullscreen mode
  1. If the remote branch is ahead of your local branch, pull the latest changes from the remote with git pull:
git pull
Enter fullscreen mode Exit fullscreen mode
  1. Run git log to identify the hash of the commit you want to revert to:
git log
Enter fullscreen mode Exit fullscreen mode
  1. Fire git revert to revert to the specified commit:
git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Note: The procedure is the same as the one outlined in the previous chapter.

  1. Upload the changes to the remote repository with git push:
git push
Enter fullscreen mode Exit fullscreen mode

FAQs

What is the difference between the git revert and git reset commands?

git revert creates a new commit that undoes one or more commits, preserving the commit history. git reset removes or modifies commits, altering the commit history.

How do you specify the name of the commit created by git revert?

Perform a revert without automatically committing, and then commit it with git commit and a custom message:

git revert -n <commit-hash>
git commit -m "Custom message"
Enter fullscreen mode Exit fullscreen mode

Is it possible to revert multiple commits at once?

Yes, by specifying a commit range as follows:

git revert HEAD~5..HEAD
Enter fullscreen mode Exit fullscreen mode

For example, the above command will create a revert commit for each of the last 5 commits.

How can git revert be prevented from opening the editor?

With the --no-edit option:

git revert --no-edit <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Conclusion

By following the procedures described in this article, you can revert commits on either a local or remote branch in your Git repository.

Top comments (0)