Git

Git – Remove Commit From History

Git commit is one of the important components of the Git program. The different developer works on the project using Git’s working repository and modifies the project’s files, functions, and features. Then, these changes are committed and saved in the Git local repository using Git commit. But occasionally, Git users may mistakenly commit the changes or commit the same changes many times, which may cause confusion. Therefore, these commits are needed to be removed from History.

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:

$ cd "C:\Git"

Step 3: Check Git Logs
Check out the Git logs by utilizing the provided command:

$ git log

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:

$ git rebase -i 2cb5bf1

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:

$ 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:

$ git log

Step 2: Remove Most Recent Commit
To remove the latest commit, execute the “git reset –hard HEAD~1” command:

$ git reset --hard HEAD~1

Again, check the Git log to verify if the recent commit is deleted or not using the “git log” command:

$ git log

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.

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.