How to Remove Git Commit that has not been Pushed?

How to Remove Git Commit that has not been Pushed?

How to Remove Git Commit that has not been Pushed?

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.

git tutorial

Remove Git Commit that has not been Pushed

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.

  • –hard
    This option resets both the index and working directory.
  • HEAD~1
    It represents the git commit just before the current one.

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.

How to recover reverted/removed git commit?

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!

Leave a Reply

Your email address will not be published. Required fields are marked *