Skip to content

Clear history commit on GitHub

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.

Make sure we stay in master branch, and pull the latest code.

Step 1: Create new branch from master branch and switch to new branch.

git checkout --orphan latest_branch
The core use for git checkout --orphan is to create a branch in a git init-like state on a non-new repository.

Without this ability, all of your git branches would have a common ancestor, your initial commit. This is a common case, but in no way the only one. For example, git allows you to track multiple independent projects as different branches in a single repository.

That's why your files are being reported as "changes to be committed": in a git init state, the first commit isn't created yet, so all files are new to git.

Step 2: Add all file to the branch latest_branch

git add -A

Step 3: Commit the changes to the branch latest_branch

git commit -am "push code"

Step 4: Delete current master the branch

git branch -D master

Step 5: Rename the branch latest_branch to master

git branch -m master

Step 6: Finally, force update your repository

git push -f origin master

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.

Comments