Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Mid-2017 answer</h2> <p>Creating a new completely empty commit with no side effects is probably best done by using Git’s plumbing directly. Doing it that way avoids any side effects: no touching the working copy or the index, no temporary branches to clean up, etc. So:</p> <ol> <li><p>To create a commit, we need a directory tree for it, so we create an empty one first:</p> <pre><code>tree=`git hash-object -wt tree --stdin &lt; /dev/null` </code></pre></li> <li><p>Now we can wrap a commit around it:</p> <pre><code>commit=`git commit-tree -m 'root commit' $tree` </code></pre></li> <li><p>And now we can rebase onto that:</p> <pre><code>git rebase --onto $commit --root master </code></pre></li> </ol> <p>And that’s it. You can rearrange that whole thing into a one-liner if you know your shell well enough.</p> <p>(N.B.: in practice I’d now use <code>filter-branch</code>. Will edit that in later.)</p> <hr> <h2>Historical answer (referenced by other answers)</h2> <p>Here’s a cleaner implementation of the same solution, in that it works without the need to create an extra repository, futz around with remotes, and correct a detached head:</p> <pre><code># first you need a new empty branch; let's call it `newroot` git checkout --orphan newroot git rm -rf . # then you apply the same steps git commit --allow-empty -m 'root commit' git rebase --onto newroot --root master git branch -d newroot </code></pre> <p>Voila, you’ve ended up on <code>master</code> with its history rewritten to include an empty root commit.</p> <hr> <p>NB.: on old versions of Git that lack the <code>--orphan</code> switch to <code>checkout</code>, you need the plumbing to create an empty branch:</p> <pre><code>git symbolic-ref HEAD refs/heads/newroot git rm --cached -r . git clean -f -d </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload