Delete all commits from git branch

How to delete all commits from a git branch.

Create a new blank branch (NEW_BRANCH)

1
2
git checkout --orphan NEW_BRANCH
git rm -rf .

Add a single file (need to have a commit)

1
2
3
touch README.md
git add README.md
git commit -m "initial"

Delete the branch you want to remove (OLD_BRANCH)

1
git branch -D OLD_BRANCH

Rename the current branch (NEW_BRANCH) to the deleted branch (OLD_BRANCH)

1
git branch -m OLD_BRANCH

Push your changes

1
git push -f origin OLD_BRANCH