Git

Remove a Git Commit Which Has Not Been Pushed

Git is a popular tool for DevOps projects used to keep track of different versions of projects and provides many features for doing so, such as a Git repository, git stash, Git branches, and many others. Moreover, Git users can easily undo or revert the changes and restore the previous version of the project by removing Git commits that have not yet been pushed.

This post will demonstrate the method for removing a Git commit that has not been pushed.

Remove a Git Commit Which Has Not Been Pushed

To remove the Git commit which has not been pushed, first, open the Git local repository. Next, utilize the “git reset” command.

To remove the Git commit, check out the below-listed procedure.

Step 1: Launch Git Terminal

Open the Git Bash terminal from the Start menu:

Step 2: Open Git Repository

Open the Git repository through the “cd” command:

$ cd "C:\Git"

Step 3: Make New File

Next, utilize the “touch” command to make a new file:

$ touch File1.txt

Step 4: Add File to Staging Environment

Add the created file to the staging environment by using the mentioned command:

$ git add File1.txt

Check the Git repository status with the “git status” command to see if the file has been added to the staging environment:

$ git status

Step 5: Commit File

To implement changes, commit the file from the staging environment:

$ git commit -m "First commit"

Check the Git repository logs to see if the changes have been committed or not:

$ git log

You can see that the file was successfully committed here:

Step 6: Remove Git Commit

In order to remove the Git commit which has not been pushed, execute the “git reset” command. This command will revert the commit to the previous commit and discard the new commit:

$ git reset HEAD~1

Check the Git logs once more to ensure that the Git commit was removed or not:

$ git log

The below output indicates that we have successfully removed the Git commit which has not been pushed yet:

Alternatively, Git users can utilize the “git reset” command along with the “–hard” option:

$ git reset --hard HEAD~1

We have demonstrated the method for removing a Git commit that has not been pushed.

Conclusion

To remove a Git commit that has not been pushed, first open the Git local repository. Next, make a new commit to implement changes using the “git commit” command. In order to remove the Git commit which has not been pushed, utilize the “git reset HEAD~1” command or the “git reset –hard HEAD~1” command. This post has demonstrated the method for removing the Git commit that has not been pushed.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.