Steps to Undo Unwanted Git Commits

Have you ever made a commit in your repository and suddenly realized, "Oh no! This should not be committed." and then felt overwhelmed by the responses on the web? Here are the basic steps that can come to your rescue.

Here, I have created a basic index.js file, which has a 'first commit' log added in the first commit and a second log added in the second commit. That's it

Step:1 First, list all the commits you have made

git reflog

Steps:2 Now, we want to remove the changes made in the second commit, so we can reset it to the first commit

git reset <your commit hast> //in this case 84bcfcd

Note: Here, we are not performing a hard reset as we still want those changes. We may want to stash them or edit the changes made in this commit and commit again.

Now, we have successfully removed the changes made in this commit. It depends on us; we can discard this change, edit it, or maybe even stash it

Are we done? Well, not yet. Let’s try pushing this to the remote repository.

This happens because our local branch is not up-to-date with the remote branch. The remote branch has commits that our local branch does not have, which is expected. So now we can just force-push our changes.

Step:3

git push origin <branch-name> --force //in this case branch-name is master

And that's it we have successfully removed the unwanted changes. Happy coding!