How to Undo Git Reset

Abdul Jabbar Feb 02, 2024
  1. The Git Reset Command
  2. Undo Git Reset
How to Undo Git Reset

When developers work on various projects, they face different situations where they make mistakes. On a significantly lighter note, everyone makes mistakes while using technology.

So, every version control system has an undo feature for this purpose. Git can also undo the last mistake feature to remove the previous check-in using undo or reset command.

We must be careful because we cannot undo some of our mistakes for various reasons. If we do it wrong, we will lose our work.

This article will look at essential tools for undoing the last check-in we did. We will see some examples and ways of undoing the git reset.

The Git Reset Command

All files on the recent branch in the repository are affected by the reset command. It is used to discard changes we haven’t committed yet into the remote branch.

The git reset command is also used to change the branch’s current head to other commits specified in the last commit to that branch.

To understand it, we will have a detailed look at Git’s three trees, i.e., Git’s internal state management system explained below.

Working Directory

The working directory must represent the local file system available to the code editor to apply the changes we want. It is the part of the Git history that the HEAD points at the specified commit into the specific branch.

Staging Index Tree

This tree keeps track of the changes done in the working directory. This will specify all the specific commits done by the team members.

Commit History

The command git commits attached the changes to a permanent snapshot kept in the branch’s commit history.

Undo Git Reset

Git is efficient in keeping a log of all reference updates that we have done in the past. It can be checkout, reset, commit, merge, check-in, pull, etc.

We can have a look at them by running the git reflog command:

git reflog

Output:

49ab051 HEAD@{0}: reset: moving to HEAD~1
b53c071 HEAD@{1}: Change the length ...

We can run the below-mentioned command to undo our mistake and go to the commit before resetting:

git reset HEAD@{1}
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 Reset