MICROFACT: How To Create An Empty Git Root Commit
// July 11th, 2012 // Uncategorized
I’ve been doing a lot of tooling and implementation with the wonderful Git revision control system. In my time, I’ve needed to do some magic things involving repository history. One thing that comes up often is the need to have an empty commit history on some branch.
This is actually pretty easy. Here’s the run-down:
git read-tree --empty NEW_TREE="$(git write-tree)" NEW_COMMIT="$(git commit-tree -m empty "$NEW_TREE")" git branch NEWBRANCH "$NEW_COMMIT" git reset
If it’s not obvious from the above code, here’s what’s going on.
- Git read-tree creates an empty index.
- Git write-tree creates an empty tree object from the empty index.
- Git commit-tree creates a new commit with that tree, no parent, and the commit message “empty”.
- Git branch creates a new branch pointing to that commit.
Sometimes, a small but very important fact is difficult to definitively find on the Internets. A microfact post exists to seed this information into Google, so that others won’t have to search as hard as I did.