How to clean the commit history of a repository?

How to clean the commit history of a repository in both of my locally cloned copy and the copy on the git server so that only the files after the last commit are left?

git works in branches. Here, we assume removing the history of the master branch. One solution is doing as follows.

Rename the current master.

git checkout --orphan newbranch

Add all files back. If you would add only some files, add them separately.

git add -A

Commit the change of adding files.

git commit

Delete the master branch.

git branch -D master

Rename the current newmaster branch to be master.

git branch -m master

Force pushing the master branch to the git server.

git push -f origin master

(Optional) Let git garbage collect unnecessary files in the local repository

git gc --aggressive --prune=all

Leave a Reply

Your email address will not be published. Required fields are marked *