LLI Wiki — My Digital Knowledge Base

Git: delete all commit history

Start over with an empty history using an orphan branch, and drop the last commit.

How to wipe the whole commit history of a Git repository while keeping the files as they are, and how to drop the most recent commit. See the original discussion.

Deleting the .git folder may cause problems in the repository. Recreating the history from an orphan branch is the safe way.

Delete the whole history

Create an orphan branch (no parent history):

git checkout --orphan latest_branch

Add all the files:

git add -A

Commit the changes:

git commit -am "commit message"

Delete the old branch:

git branch -D master

Rename the current branch to master:

git branch -m master

Force-update the remote repository:

git push -f origin master

Delete the previous commit

Undo the last commit while keeping its changes staged:

git reset --soft HEAD^

Alternatively, open an interactive rebase on the last two commits and delete the second line in the editor that opens:

git rebase -i HEAD~2

Notes

Mastodon LLIMastodon SocialFosstodon