How To Revert A Commit In Git?

508 Views
0 rating based on 0 reviews

Reverting a commit is quite simple with the help of Git. The idea of reverting a commit could be a bit difficult for newbies. Let us quickly understand the basic usage of revert in Git. Suppose we have committed fifty files and then mistakenly deleted those files and wish to revert the changes. This can be done from terminal by following these simple steps.

Revert a Commit in Git

Pushing too many commits in the repository is no surprise for pros. And many situations require undoing the action of published commits.

Whether it’s additional changes or wasteful commit – you’ll have to start restructuring the project. It explicitly needs the use of revert.

There are different ways to implement the change in your repository. In-depth details on the commit revert start coming right away.

How Git Revert Works?

Git revert is more like an ‘undo’ operation, deviating from the original meaning. It doesn’t remove a particular commit from your project history.

Instead, it manages to invert the commit changes in a plausible way. As it happens, the resulting git content gets to append a new commit.

The feature explicitly concerns an inverse commit requirement. It keeps your Git project history safe, letting you collaborate with the integrations.

Git Revert Command Action

Reset vs. Revert

Many users get confused over revert operation with a reset on restoring the previous commit. You must catch the distinctions to avoid time-consuming mistakes.

Reverting action in Git aims at individual commit at the history’s arbitrary point. No change in the Git history manages to secure the published repository commit.

Meanwhile, reset is a broad reverting version to occupy the previous project state. It explicitly takes out all the subsequent commits, affecting the repository.

Git Reset vs Git Revert

Undo the Last Commit with Revert

For the current upgrades, you can use two different methods for the action. Command revert remains old-school, whereas modern layout allows single-click revert.

Initializing Revert from Branch

Direct execution is available for Tower Git Client users. Some advanced versions may facilitate the feature to a limited extent.

  1. Whether it’s macOS or Windows, the process remains simple & effortless. Some mere clicks will execute the change in the same manner.
  2. Enter your branch to locate ‘History’ right on its top section. Select the commit to make a right-click. The options will show ‘Revert this Commit’.
    Commit History from Branch
  3. Press the button to undo your commit using the right-click menu. For reverting multiple commits, sort the commits from ‘Newest to Oldest’.
    Revert Commit with Simple

Initiating Revert from Command

Using the command to launch a specific program remains the absolute basic. You can do almost anything through sequential execution.

  1. Utilize the ‘git revert’ command to specify the targeted commit. For the template command, the commit goes under the term ‘Head’.
    Git Revert Command
  2. Execution of git revert command will automatically open the text editor. After completing the changes, a new message will get displayed.
    New Message Upon File Removal
  3. You can check the Git history to confirm a successful revert. It should show a completely new commit for undoing the repository one.

Undoing the Last Commit with Reset

Execution of the ‘git reset’ command is the simplest way to undo the very last commit. It requires an additional ‘—soft’ command. The program will preserve the changes that appeared on the files.

Soft Reset to Preserve the Change

Of course, you should get familiar with the ‘Head~1’ notation. It reveals your intention to undo the HEAD to a commit before log history.

But you may not require to preserve the performed changes all the time. Use ‘—hard’ instead of ‘—soft’. The executed program won’t save anything, getting rid of the commit preservation.

Hard Reset to Skip the Change

Unlike the reset function, revert can help you to undo a particular commit.

Summary: Revert a Commit in Git

  • Use the git reflog command to browse through a history of your local repository. This will show you a log of commits with their commit ID and message.
  • Once you have found the commit ID, you need to revert back to that point using the following command and replacing the hash with the commit ID we just found: