Ultimate Guide on How to Delete Commit History in Github

MehmoodGhaffarMemon
3 min readMay 22, 2023

--

Step by step guide to delete unwanted commit log from commit history to remove sensitive information from remote repository

Are you looking to clean up your Git repository by removing unwanted commits and start afresh? If so, you’ve come to the right place. In this guide, we will show you how to delete commit history in Github.

Why Delete Commit History?

Before we delve into the details of deleting commit history in Github, it’s important to understand why you might want to do this. Here are some reasons:

  1. Clean up your repository: If your Git repository has a cluttered history, deleting unnecessary commits can help clean it up, making it easier to manage.
  2. Remove sensitive information: Sometimes, you may accidentally commit sensitive information, such as passwords or API keys. Deleting these commits will ensure that this information is no longer visible in your repository.
  3. Start afresh: If you’ve made a lot of experimental changes to your repository, deleting the commit history will give you a clean slate to work with.

Now that you know why you might want to delete commit history, let’s get started.

Step 1: Backup Your Repository

Before you start deleting commits, it’s important to backup your repository. This will ensure that you have a copy of your repository in case anything goes wrong during the process.

Step 2: Identify the Commits to Delete

To delete commit history, you need to identify the commits that you want to remove. ‘git log’ will display all the commit log that You have in your project repository.

For example, to list the last 10 commits, you can run the following command:

git log -10

Step 3: Remove Commits Using Git Rebase

Once you’ve identified the commits that you want to delete, you can use the git rebase command to remove them.

For example, if you want to remove the last three commits, you can run the following command:

git rebase -i HEAD~3

This will open an interactive rebase window, where you can select the commits that you want to delete. To do this, simply delete the lines that correspond to the commits that you want to remove.

Once you’ve deleted the lines, save and close the file. Git will then remove the selected commits.

Step 4: Force Push Changes

After you’ve removed the commits, you need to force push the changes to the remote repository. You can do this using the following command:

git push origin +master

Note that the + sign before the branch name is important, as it tells Git to overwrite the remote branch with your changes.

Step 5: Verify Changes

Finally, you should verify that the commit history has been deleted successfully. You can do this by using the git log command again.

For example, to list the last 10 commits after deleting the last three, you can run the following command:

git log -10

This should show that the commits you deleted are no longer present in the commit history.

Conclusion

Deleting commit history in Github can help you clean up your repository, remove sensitive information, and start afresh. However, it’s important to backup your repository before you start and to be careful when using the git rebase command.

By following the steps outlined in this guide, you can confidently delete commit history in Github and enjoy a clean state to work with.

--

--

MehmoodGhaffarMemon

Tech-savvy writer sharing knowledge on fullstack-dev, DevOps, and ethical hacking. Bringing insights and solutions to fellow tech enthusiasts