DEV Community

Cover image for Git Reset vs. Git Revert: When to Use Each Command
Rahul for Fueler - Your Developer Portfolio

Posted on • Originally published at fueler.io

Git Reset vs. Git Revert: When to Use Each Command

Create your Dev Portfolio today at Fueler.


Have you ever committed changes to a repository, only to realize that it wasn't what you meant to do?

Don't worry—we've all been there.

The good news is that there are smarter ways of undoing commits than simply rolling back the entire repository.

At its core, Git is a version-control system that lets you track and manage changes in your projects.

It offers several powerful commands which can help you undo commits and restore a clean state for your project.

These commands are Git Reset and Git Revert.

But what’s the difference between them? When should you use each command?

In this article, we'll look at both Git Reset and Git Revert in detail and explain when it’s best to use each one.

That way, you’ll never have to worry about messing up your commits again! Let's start by learning about the basics of Git Reset vs Revert.

What Is Git Reset?

Git reset is one of the most powerful commands in git. It can help you reset your local branch to any commit in the branch's history, going back in time as far as you need.

There are three types of Git reset:

  • Soft: This just resets the pointer that references your commits to whatever you specify (for example, a previous commit). Your working directory and staging area aren't changed at all.

  • Mixed: The mixed option moves your HEAD pointer back but leaves everything else untouched. In other words, any committed changes are still saved, but the staging index and working directory are updated with the contents of HEAD at that commit.

  • Hard: If you want to erase all commits after a certain point, the hard option is your best bet. Once applied, any changes committed after the target commit are permanently discarded. It’s important to note that this resets both your current branch and staging index and makes them look exactly like they did when you set it back.

Git reset is best used when you need to undo changes made in a single file (or fewer) and don’t want to discard any of your other work or commits. But be careful—a hard reset can cause serious headaches if used improperly!

Code example: how to perform a soft reset using Git command line

  1. First, open your terminal and navigate to the repository you want to work with.

  2. To perform a soft reset, use git reset --soft HEAD~1 command

This will undo the last commit, but keep the changes in the staging area so that you can modify them and commit again.

  1. To check the status of your repository after the reset, use the git status command. This will show you the files that were changed in the last commit, which are now in the staging area.

  2. To modify the changes and commit again, use:

    git add <file-name>

    git commit -m "New commit message"
Enter fullscreen mode Exit fullscreen mode

This will add the modified changes to the staging area and create a new commit with a new message.

Note: It's important to be cautious when using git reset as it can delete commits and modify the history of your repository.

Always make sure to backup your work before using this command, and consider using git revert instead if you want to undo a commit without modifying the history.

What Is Git Revert?

Git revert is a command that allows you to undo a specified commit while preserving the rest of your history in the repository. It creates a new commit that undoes some or all changes of the specified commit, making Git revert an ideal way to undo a specific change to your repository.

When should you use Git revert?

Generally speaking, it's an effective way to undo changes in certain scenarios.

  • For example, if you've pushed code that breaks the build, has unforeseen bugs, or violates another team member’s work, Git revert should be your go-to command.

However, there are some risks and precautions to consider when using Git revert.

Like for example, always double-check that you're reverting the correct commit – if not, it could introduce its own bugs and issues.

It's also important to make sure there aren't any conflicts between branches with similar commits; this could cause problems when trying to merge branches in the future.

Overall, Git revert is a great tool for discreetly undoing changes and keeping your repository clean and organized. When used properly and with caution though, it can save you from any major blunders or missteps down the road.

How to revert a commit using Git command line

  1. First, use the git log command to find the commit hash that you want to revert. The commit hash is a long string of characters that uniquely identifies the commit.
commit 946763c97874be293696ba2c119c97ff1b7cb3
Author: rahuldotbiz <gamerrahul1122@gmail.com>
Date:   Thu Mar 16 14:02:46 2023 +0530

    analytics removed
...
Enter fullscreen mode Exit fullscreen mode
  1. Once you have identified the commit hash, use the git revert command followed by the commit hash to create a new commit that undoes the changes made in the specified commit.

    git revert 946763c97874be293696ba2c119c97ff1b7cb3
    [master 4387c34] Revert "Add new feature"
     1 file changed, 1 deletion(-)
    
  2. Git will open up a text editor where you can enter a commit message describing the revert. Once you save and close the editor, Git will create a new commit that undoes the changes made in the specified commit.

    Revert "Add new feature"

    This reverts commit 946763c97874be293696ba2c119c97ff1b7cb3.

    Reason for revert: This feature caused a regression in the application and needs to be reworked.
Enter fullscreen mode Exit fullscreen mode
  1. Finally, use the \git push\ \ command to push the new revert commit to the remote repository.

Comparing Git Reset and Git Revert

When undoing a commit, it's important to know the differences between git reset and git revert. They seemingly do the same thing, but their impacts are very different.

Git Reset

Git reset is a powerful command that should be used with caution. It moves the HEAD and the current branch pointer to the desired commit.

One of its benefits is that it affects only your local repository, while changes can be pushed to remote repositories with git push.

However, using this command has certain drawbacks as well—if you want to go back further than what you've already committed, it can only be done from the staging area.

Git Revert

Unlike git reset, git revert does not move any of your branches or change anything about them—it just creates a new commit that is the opposite of the one you specify.

In other words, if you have a commit that adds some lines of code, running git revert on that commit adds another commit that removes those lines again.

This allows for more flexibility in terms of going back further in your history and making changes to existing commits.

When it comes to making changes in your repository, it's important to understand when it's appropriate to use one command over another — otherwise, you could end up with unexpected results.

Of course, both commands can be used on local or remote repositories but there are certain circumstances in which one is better than the other.

Knowing this will help make sure you're making the right choices when using either command.

Conclusion

To recap, if you want to make an old commit disappear from Git history, use git reset. If you just want to leave a commit intact but undo the changes it introduced, use git revert.

As a general rule of thumb, try to use git revert first. If you’re dealing with a messy merge conflict, though, you may want to use git reset to go back to a known good state before trying the merge again.

Just remember that using git reset to undo commits is a bit more risky and should be used with caution.

Understanding the differences between “git reset” vs “git revert” can save you a lot of time and headaches in the long run.

But in the next, we'll learn more about how to undo multiple commits using Git interactive rebase.


Create your Dev Portfolio today at Fueler.

Top comments (0)