I want to delete all commit history but keep the code in its current state because, in my commit history, there are too many unused commits.
How can I do it?
Is there any git command can do this?
git filter-branch ?
git rebase ?
...
My code is hosted on github.com.
2 Answers
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:
Checkout
git checkout --orphan latest_branchAdd all the files
git add -ACommit the changes
git commit -am "commit message"Delete the branch
git branch -D mainRename the current branch to main
git branch -m mainFinally, force update your repository
git push -f origin main
PS: this will not keep your old commit history around
If you are sure you want to remove all commit history, simply delete the .git directory in your project root (note that it's hidden). Then initialize a new repository in the same folder and link it to the GitHub repository:
git init
git remote add origin :user/repo
now commit your current version of code
git add *
git commit -am 'message'
and finally force the update to GitHub:
git push -f origin master
However, I suggest backing up the history (the .git folder in the repository) before taking these steps!