How can I completely empty the master branch in Git?

后端 未结 6 2309
猫巷女王i
猫巷女王i 2020-12-23 13:41

I would like to completely empty the master branch in Git. For now, I would also like to keep all other branches which have been branched from master.

Is this possib

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-23 14:27

    Other answers suggest creating a fresh master branch (as you would have in empty repository). But having master without history is not a good practice. It makes it hard to use rebase as you can never rebase the first commit. The first commit of master on any repository should be empty. There is a low-level plumbing solution to this:

    Fix using low-level plumbing commands

    First you get an empty tree hash:

    $ git hash-object -t tree /dev/null
    4b825dc642cb6eb9a060e54bf8d69288fbee4904
    

    Then you use the tree hash to create an empty commit

    $ git commit-tree -m "inital commit" 4b825dc642cb6eb9a060e54bf8d69288fbee4904
    a5c0737b92e5e5d4502e15b93d7a46d1e17b2b22
    

    And finally you reset master to that commit

    $ git reset --hard a5c0737b92e5e5d4502e15b93d7a46d1e17b2b22
    

    It's further described in my blogpost.

提交回复
热议问题