How to Revert Commit Local in Git

Abdul Jabbar Feb 02, 2024
  1. Undo Commit Changes Using git reset
  2. Undo Commit Changes Using git revert
How to Revert Commit Local in Git

When a software engineer or web developer works with Git, it is obvious that he pushes a lot of codes and commits to the Git repository daily, and while doing it, the need to undo or revert a particular commit or a set of commits will arise from time to time in a team environment.

In this piece of block, we will learn about the available possibilities of undoing Git programs and commands once the commit has been done to the local branch.

This document attempts to understand the situation of the commit that we made and what kind of undo we can make to revert the commit that is not synced with the remote branch yet.

Before handling the situation, we should ensure that we need it and want to modify the commit files and information. Because in some cases, we only want to edit our commit, which can be done through Git’s command git amend.

But if we want to undo the command, this guide is the best place to learn about it. Let’s learn how to do it effectively using the Git command below.

Undo Commit Changes Using git reset

There are multiple situations where we want to undo that last commit, and the simplest way to sort out the matter and undo the last Git commit is to use the command git reset with the flag --soft added to it. This feature of Git will preserve changes to our files in the local branch.

For this purpose, we have to specify the commit that we want to undo, i.e., HEAD~1; here, we want to undo the last commit as it’s the common practice that developers need to undo the last commit, most probably. But we can mention any commit we want to undo.

For these cases, git reset is the best choice, and the command is listed below.

$ git reset --soft HEAD~1

Through the reset option, our current HEAD branch will be projected to the designated revision and returned to the one commit before the recent revision, this makes our last commit undone, and the changes will automatically come to our files modified section.

And with the help of the flag --soft, the changes we have made are preserved in the files status section and are not lost.

Furthermore, if we don’t want our changes to be preserved and we are sure that we don’t need them anymore, then we will use the flag --hard instead of the flag --soft.

This will permanently undo changes, and everything will be lost. For deleting the changes permanently, the command is below.

$ git reset --hard HEAD~1

Undo Commit Changes Using git revert

The git revert command is particularly used to develop a new commit that helps us in reverting the changes of the commit that is specified. This command is well known for totally reverting a commit without deleting it.

If the commit is already pushed to the remote repository, it’s the best and safest option to use git revert as it doesn’t overwrite commit history.

The command to revert the last commit is below.

git revert <commit to revert>

The name of the commit is the commit id that we want to revert; it can be recovered through the command of Git, which is the git log. Most developers prefer git revert over git reset because it undoes the changes with the help of the new command that proceeds the same undo operation.

It does not discard or overwrite previous commits. While reset discards all records of the unappealing commit and also changes the commit history.

Now, it’s up to you which command is best for you in your case.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Revert