Colors of Noise

agx@sigxcpu.org

Truncating git history
21st February 2014

When starting to work on a new project I start from an empty git repository right away so I can try out different ideas, revert easily, can diff against old versions (to check if I missed something) and have a commit history to record fixmes and todos. However when making the repo public these things are not of much interest anymore so I truncate the history. To be on the save side I want to keep that history locally though. Assuming the repo is on master, I do:

# 1.) Move the old master out of the way
git branch -m branch master oldmaster
# 2.) Get the current tree at HEAD
tree=$(git rev-parse HEAD^{tree})
# 3.) Create a new commit without ancestry
commit=$(git commit-tree -m "Initial commit" $tree)
# 4.) Make this the new master
git branch master $commit
# 5.) Switch to the new branch
git checkout master

Done. One can now add a remote and push the master branch. The old history is still there locally on the completely detached commit history ending at oldmaster:

$ git log --pretty=short --graph --decorate master oldmster

* commit d5cf3371eaefbaa8efac10c0fb9e7597da17b423 (HEAD, master)
  Author: Guido Günther <agx@sigxcpu.org>

      Initial commit

* commit 64103ff72bde13d7ec4cf0489ad2a80f3ac249d3 (oldmster)
| Author: Guido Günther <agx@sigxcpu.org>
|
|     Another uninteresting commit message
|
* commit bd7332a79380bb217eca09cbd7f6ff0e5174deb8
| Author: Guido Günther <agx@sigxcpu.org>
|
|     Uninteresting commit message
|
... <more old history>

Update: Uli Heller pointed out that this is the same as using git checkout's --orphan option.

Tags: planetdebian, planetfsfe.

RSS feed