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}
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.
LinkedInRelated Article - Git Reset
- Difference Between the Git Reset, Revert, and Checkout Commands
- How to Make the Development Branch Identical to the Master Branch
- How to Remove Local Git Changes
- How to Revert a Git Merge With Conflicts
- Difference Between Git RM --Cached and Git Reset File
- How to Revert a Git Repository by Commit ID