How to Undo Commit in Git
- Undo Commit in Git - Hard Reset
- Undo Commit in Git - Reset Git
- Undo Commit in Git - Soft Reset Git
- Undo Commit in Git - Recover Hard Reset Files
This article is about to guide us on how to recover what we don’t mean to do that while using Git commit. Git is not that complicated that we need a large process to tackle our particular problem, but it is more than the problem that is caused is so large that different techniques are required depending on what we have the issue and what we want result.
There are many aspects of Git that are easy to understand, but getting back to where we were before is much easier and appreciable; it is easier to undo major changes in a repository. It is a little scary moment when we don’t know how it is performed using commands. But actually, it is surprisingly easy to do small things like commit or undo the commits.
But what you would do in this case to undo your unwanted commit and then commit your new changes.
Let’s say that if you made some changes and then committed the changes, for that purpose, there are four methods to do this:
Undo Commit in Git - Hard Reset
If you have this, where 40 is your HEAD
and (42) is the state of our files.
(42)
38-39-40
↑
Head
And you want to undo commit 40 and never want to see it again and erase all the changes in locally modified files.
We will use the following command:
git reset --hard HEAD ~ 1
Output:
(42)
38-39
↑
Head
Now 39 is the HEAD
. Because in the above example, we used --hard
, now your files are reset to their state at commit head which is 39th commit.
Undo Commit in Git - Reset Git
Let’s suppose the last commit 40 wasn’t a big deal, but just a bit off. Now we want to undo the commit but keep our changes. Starting again from here, with 40 as our HEAD
:
(42)
38-39-40
↑
Head
Rather than using --hard
parameter, we will use the following command:
git reset HEAD ~ 1
Output:
(42)
38-39-40
↑
Head
As we have seen in both cases, HEAD
just defines the latest commit. When we do a git reset HEAD~1
, the command Git moves the HEAD
pointer back to one commit. But (unless you use --hard
) we leave our files as they were earlier. We haven’t lost anything in it.
Undo Commit in Git - Soft Reset Git
On a lighter note, you can even undo our commit but leave our files and our index using the following:
git reset --soft HEAD ~ 1
This will not only leave our files there; it even leaves our index there alone. When we do git status
, we’ll see that the same files are in the index as before. In fact, we will see after this command, we could do git commit
and would be doing again the earlier commit we just had.
Undo Commit in Git - Recover Hard Reset Files
Suppose we undo a commit as in the first option example but then realize that we needed it afterward. Now, what to do?
Don’t worry, and there’s still a way to get it back. We will use the command git reflog
and see a list of (unfinished) commits has that we’ve moved around in. Find the commit we destroyed, and do this:
git checkout -b BranchName CommitYouDestroyed
We’ve now restored that commit. In Git, commits don’t delete for the 90 days from the day it was deleted, so we can easily restore it as a backup again into the repositories.
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