Clear Commit History - egnomerator/misc GitHub Wiki
This link is to a Stack Overflow post asking
How to squash all git commits into one?
- If performing the below operation(s) results in re-writing public history, then pushing changes with require the force flag
note: after performing this operation, to apply the changes to the remote branch, do a force push
My personal use case for this action was the following:
- I had a long commit history in my git repository for this github wiki
- I wanted to just clear the commit history and continue from a "clean start"
- I don't see a likely scenario where this operation would be desireable in a professional context (not saying never, just not likely or common)
In case the included links ever break, here is the pertinent information from the Stack Overflow post
How to squash all git commits into one?
How do you squash your entire repository down to the first commit?
I can rebase to the first commit, but that would leave me with 2 commits. Is there a way to reference the commit before the first one?
My chosen answer (also in top section "helpful links")
- more than one answer was given, but I found this one best for my purposes
- specifically Alternative solution #1: orphan branches
If all you want to do is squash all of your commits down to the root commit, then while
git rebase --interactive --root
can work, it's impractical for a large number of commits (for example, hundreds of commits), because the rebase operation will probably run very slowly to generate the interactive rebase editor commit list, as well as run the rebase itself.
Here are two quicker and more efficient solutions when you're squashing a large number of commits:
You can simply create a new orphan branch at the tip (i.e. the most recent commit) of your current branch. This orphan branch forms the initial root commit of an entirely new and separate commit history tree, which is effectively equivalent to squashing all of your commits:
git checkout --orphan new-master master git commit -m "Enter commit message for your new initial commit" # Overwrite the old master branch reference with the new one git branch -M new-master master
Documentation:
Another efficient solution is to simply use a mixed or soft reset to the root commit
<root>
:git branch beforeReset git reset --soft <root> git commit --amend # Verify that the new amended root is no different # from the previous branch state git diff beforeReset
Documentation: