Clue Mediator

Delete all commit history in GitHub

📅December 23, 2020
🗁Git

Today we’ll show you how to delete all commit history in GitHub. It's a very small but useful article to delete all commit history of the branch and create a fresh commit using git command.

Here, we’ll show you the two different methods to remove all commit history using git commands without removing the repository.

Ways to delete all commit history in GitHub

  1. Method 1: Using an orphan branch
  2. Method 2: By deleting the .git folder

Method 1: Using an orphan branch

In case, if you are facing any issue to delete the `.git` folder then you can use this approach. With the help of this method, we can keep the code in its current state.

# Checkout to a new temporary branch
git checkout --orphan TEMP_BRANCH

# Add all the files (If not added)
git add -A

# Commit the changes in the temporary branch
git commit -am "Initial commit message"

# Delete the old master branch
git branch -D master

# Rename the new temporary branch to master
git branch -m master

# At last, Force update to our repository
git push -f origin master

If this method does not work, try the following method.

Method 2: By deleting the .git folder

In this method, we will remove the git commit history by deleting the `.git` folder because all the committed history are in the `.git` folder. Follow the steps to delete all history.

# Clone the repository, e.g. `myrepo` is my project repository
git clone https://github/cluemediator/myrepo.git

# Jump to the repository folder
cd myrepo

# Delete the `.git` folder
rm -rf .git

# Re-initialize the repository
git init
git remote add origin https://github.com/cluemediator/myrepo.git
git remote -v

# Add all the files and commit the changes
git add --all
git commit -am "Initial commit message"

# Force update to the master branch of our project repository
git push -f origin master

You might need to configure username and email and provide the credentials for your GitHub account. You can also check the list of git commands for your regular use.

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂