Javaexercise.com

How to undo the most recent local commits in Git?

Git Commits are a way to capture and store the state of our project in the repository. We can think of them as a snapshot of our project at a particular instant.

One can go back to any of these commits and examine the changes made. However, we sometimes may commit some unnecessary or irrelevant changes. But Git provides us the flexibility to undo commits.

In this article, we will learn how to undo the most recent commits. There are a few different cases, and we will look at different scenarios in which we want to undo the last commit.

Case 1: Using Git Reset --hard Command To Undo Git Commit

We can use the git reset command to undo the last commit. The git reset is used to undo changes in our repository. It moves the HEAD and the current branch reference to a previous commit point. The --hard option will delete the previous commit. It will also remove all the modifications to the files that we made. Our repository will be back to the second most recent commit state.

git reset --hard HEAD~1

For example, consider we had three commits in our local repository and want to delete the last commit(commit C). The Git reset --hard command will delete commit C and the modifications to the files made after commit B. All the changes in the working directory and the staging area will be undone.

A -- B -- C <- HEAD, Master     (Initial State)
A -- B <- HEAD, Master          (After Git Reset)

Case 2: Using Git Reset To Undo Git Commit

The --hard option used in the above scenario will undo the previous commit but, it will also remove all the modifications that we made to our files. We can omit the --hard option to undo the last commit but keep the changes.

In technical terms, the commit is deleted from the repository, and the changes will be unstaged(removed from the index or staging area). But our working directory or working tree remains intact. The state of the files on our disk will not be altered.

It will be helpful when we want to add something to our files before making a commit. We can undo the last commit, modify our files, add the files to the staging area(Git Add), and finally commit the correct changes using Git Commit.

Note that the git reset command uses the --mixed option as default. We don't need to mention it explicitly.

git reset HEAD~1
git reset --mixed HEAD~1 # --mixed is the default option used by git reset

For example, consider we have a file demo.txt with the following three lines.

This line was added in first commit(Commit A).
This line was added in second commit(Commit A).
This line was added in third commit(Commit C).

We have three commits in our repo, and we want to remove the last commit(Commit C).

A -- B -- C <- HEAD, Master     (Initial State)

But we don't want to undo any changes in the demo.txt file. Git Reset command is used for this purpose. After using the command:

This line was added in first commit(Commit A).
This line was added in second commit(Commit A).
This line was added in third commit(Commit C).
A -- B <- HEAD, Master      (After Git Reset)

Case 3: Using Git Reset --soft command To Undo Git Commit

The --soft option will undo the previous commit but keeps the working directory and the staging area or index unchanged. The commit is removed from the repository. But we still have the modifications that we made to our files. In addition, the staging area is also not cleared. We can use this when we want to add more files to the commit.

git reset --soft HEAD~1

Case 4: Undoing Multiple Commits in Git

We can undo multiple previous commits with the help of the Tilde(~) symbol. The HEAD~n will undo the last n commits. For example, we will use the following command to remove the last three commits from our repository.

git reset HEAD~3

We can also use the --soft and --hard options according to our needs.

Conclusion

Commits are a way to record the changes in the version history of our projects. We can compare the current state of our repository with previously committed versions. Commits also help us roll back to the prior versions of our project and build something on top of them.

Git provides the Reset command to undo the previous commits. We can also undo the file modifications by using the --hard option. The --soft option will keep the file changes and not alter the index or the staging area. By default, the Git Reset command uses the --mixed option. This option will undo the last commit and unstage all the changes. But the working directory or working tree remains the same.