How can I delete old commits in Github via terminal?
Mia Lopez
I have a repository.
How can I delete my old commits via terminal? My SSH key is uploaded.
(I'm using github pages, that's why version tracking is not important, don't want to waste Github's storage)
13 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 masterRename the current branch to master
git branch -m masterFinally, force update your repository
git push -f origin masterPS: this will not keep your old commit history around.
You have an really good answer for that here:
And I quote
Note: please see alternative to
git rebase -iin the comments below—
git reset --soft HEAD^First, remove the commit on your local repository. You can do this using
git rebase -i. For example, if it's your last commit, you can dogit rebase -i HEAD~2and delete the second line within the editor window that pops up.Then, force push to GitHub by using
git push origin +master.See Git Magic Chapter 5: Lessons of History - And Then Some for more information (i.e. if you want to remove older commits).
Oh, and if your working tree is dirty, you have to do a
git stashfirst, and then agit stash applyafter.
Hope this help you :)
==== EDIT ====
Actually I just reread the post I linked to and specially the comment "word of caution 2"
From that I learned that git seems create a new commit and moving the pointer to it, rather than deleting it when using force push. That is if someone already have the SHA1 of your commit, they can still access it. So when taking this into consideration, I must recommend that you do as Tomasz Klim suggested.
- Create new repository.
- Copy all files to it.
- Destroy old repository.