2 min read

How to delete all commit history in git/github

An archived Git history beside a fresh root commit and preserved file cards.

How to delete all commit history in git/github

Deleting the .git folder may cause problems in your git repository. If you want to delete all your commit history but keep the code in its current state, it is very safe to do it as in the following:

  1. Checkout
git checkout --orphan latest_branch
  1. Add all the files
git add -A
  1. Commit the changes
git commit -am "commit message"
  1. Delete the branch
git branch -D main
  1. Rename the current branch to main
git branch -m main
  1. Finally, force update your repository
git push -f origin main

Sometimes you may need to create a brand new branch on a git project that doesn't contain anything present on the other branches nor shares an history with them, called orphan branch.

This snippet describes how to do it:

BRANCH_NAME=$1
git checkout --orphan $BRANCH_NAME
git rm -rf .
rm .gitignore
echo "# $BRANCH_NAME" > README.md
git add README.md
git commit -m "Initial commit for branch $BRANCH_NAME"
git push origin $BRANCH_NAME

This snippet can be used as a sequence of commands, replacing $1 with any value for the branch name, or in a bash script, for example naming it orphan_branch.sh and running it with sh ./orphan_branch.sh new_branch_name.

for more info about --orphan: https://git-scm.com/docs/git-checkout

  • Growth5 min read ↗
  • Engineering

    Git Stash

    Git stash is a built-in command with the distributed Version control tool in Git that locally stores all the most recent changes in a workspace and resets the state of the workspace to the prior commit state.

    2 min read ↗
  • Engineering

    Git Branching

    Branching is a feature available in most modern version control systems.

    3 min read ↗