This tutorial will help you to remove the git commit that has not been pushed.
Note: If you have already pushed the command to the remote repo and want to revert it, check the git reset commit from the remote repo.
Here are the commands and steps that involve reverting the git commit.
Run the below git reset command:
git reset --hard HEAD~1
It moves the head pointer of the current branch back by one commit.
Make sure you are on the right branch before executing this command.
After running this command check the changes in the working directory, just to make sure that the branch is reverted to the correct commit.
As the reverted commit is not pushed earlier, you don’t need to push any changes after reverting the commit.
These steps are very useful incase if you commit by mistake and don’t want to push it to the remote repo.
Let’s say you have reverted the git commit. But now you realize, you removed the wrong commit. There is a way to recover your git commit.
Run reflog command
git reflog
It will give you the commit’s SHA-1 hash.
Use this hash with the reset command as
git reset --hard <commit-hash>
It will recover the commit corresponding to the commit hash.
If you have any difficulty, let me know in the comment section below. Happy coding!