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.
Typically, you should never delete your commit history. However, if you're working on your own project or a repository that isn't used by many people, you might want to delete your commit history without touching the code.
git rebase -i HEAD~<number of commits to go back> , so git rebase -i HEAD~5 if you want to see the last five commits. Then in the text editor change the word pick to drop next to every commit you would like to remove. Save and quit the editor. Voila!
Each clone usually includes everything in a repository. That means when you clone, you get not only the files, but every revision of every file ever committed, plus the history of each commit.
To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.
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_branch
Add all the files
git add -A
Commit the changes
git commit -am "commit message"
Delete the branch
git branch -D main
Rename the current branch to main
git branch -m main
Finally, 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 [email protected]: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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With