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:
Step 3: Make New File
Next, utilize the “touch” command to make a new file:
Step 4: Add File to Staging Environment
Add the created file to the staging environment by using the mentioned command:
Check the Git repository status with the “git status” command to see if the file has been added to the staging environment:
Step 5: Commit File
To implement changes, commit the file from the staging environment:
Check the Git repository logs to see if the changes have been committed or not:
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:
Check the Git logs once more to ensure that the Git commit was removed or not:
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:
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.