DEV Community

Cover image for How to delete entire git commit history?
Axorax
Axorax

Posted on • Edited on

How to delete entire git commit history?

I have deleted my git commit history plenty of times just to make my repository look "clean" (roast me in the comments for it). It's probably best to not do this but I will still share how to do it.

Delete entire commit history

git checkout --orphan latest_branch
Enter fullscreen mode Exit fullscreen mode
git add -A
Enter fullscreen mode Exit fullscreen mode
git commit -am "commit message"
Enter fullscreen mode Exit fullscreen mode
git branch -D main
Enter fullscreen mode Exit fullscreen mode
git branch -m main
Enter fullscreen mode Exit fullscreen mode
git push -f origin main
Enter fullscreen mode Exit fullscreen mode

I did not figure this out myself or anything. I got it from here. It's by Desta Haileselassie Hagos, later edited by creyD

Personally, I don't like to type out every single one of those commands. So, I made a Git alias that allows me to do all of that in one single command. Using my Git alias the command becomes:

git renew "sub to axorax"
Enter fullscreen mode Exit fullscreen mode

To make the Git alias for yourself, paste the command below in the terminal:

git config --global alias.renew '!f() { \
  git checkout --orphan latest_branch && \
  git add -A && \
  git commit -am "$1" && \
  git branch -D main && \
  git branch -m main && \
  git push -f origin main; \
}; f'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)