This write-up will elaborate on how to remove commits from history.
How to Remove Commit From History in Git?
To remove any specific commit from history, first, open the Git repository. Edit the commits through the βgit rebaseβ. After that, the commits file will open in the default selected editor, from where you can remove the commit from the file and save it.
For removing a commit from history, take a look at provided instructions.
Step 1: Open Git Terminal
First, open the Git terminal from the βStartβ menu:
Step 2: Open Git Repository
Next, move to the Git repository through the βcdβ command:
Step 3: Check Git Logs
Check out the Git logs by utilizing the provided command:
Step 4: Edit Commits History
Use the βgit rebaseβ command to erase the commit from history and specify the commit id of the commit that is closest to the one that needs to be deleted. Here, the option β-iβ is utilized to edit the commit file:
Step 5: Remove Commit
Upon doing so, the commit file will open in the default selected editor. Remove the commit you want to delete. For instance, we have removed the βbe7d3e3β commit:
After that, press the βCtrl+Sβ button to save changes. To exit the nano editor, hit the βCtrl+Xβ key:
Now, verify if the commit is removed from history or not by viewing the Git log:
You can see that we have successfully removed the commit from history:
How to Remove Most Recent Commit From History?
To remove the last commit from the commit history, go through the provided steps.
Step 1: Check Git Log
Firstly, check the Git logs to check the most recent commit from history:
Step 2: Remove Most Recent Commit
To remove the latest commit, execute the βgit reset –hard HEAD~1β command:
Again, check the Git log to verify if the recent commit is deleted or not using the βgit logβ command:
We have elaborated on how to remove the commit from history.
Conclusion
Conclusion
To remove the commit from history, first, open the Git repository. Next, edit the commit through the βgit rebase -i <commit-id>β command. Upon doing so, the commit file will open in the default selected editor. Remove the commit and save the changes. Whereas to remove recent commits, utilize the βgit reset –hard HEAD~1β command. This post has elaborated on how to remove the commit from history.