How can I completely empty the master branch in Git?

后端 未结 6 2307
猫巷女王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:07

    You could try to reset the master branch to the first commit with git checkout master; git reset --hard ${sha-of-first-commit} and then amend that first commit to remove the file s in it.

    This should give you a pristine master branch, and leave all the others untouched, but since you are rewriting history, all repository that cloned yours will need to be fixed.

    0 讨论(0)
  • 2020-12-23 14:12

    That's actually called "delete old master branch and create new from scratch"

    This will create a new master branch pointing to initial commit:

    git branch -D master
    git checkout -b master <initial commit hash>
    

    This will create a totally new master branch unrelated to whatever you had:

    git branch -D master
    git checkout --orphan master
    git rm -rf *
    

    But actually you can simply save current repository to some other place and create a new repository instead.

    0 讨论(0)
  • 2020-12-23 14:13

    how about checkout to the master branch of your local repository and delete everything. then push this branch to the remote branch(origin)

    do u remove empty the content of the master branch or the branch itself?

    0 讨论(0)
  • 2020-12-23 14:14

    Create an Orphan Branch

    First, you need to move or delete your current master branch. Personally, I prefer to move it aside rather than delete it. After that, you simply create a new branch with no parents by using the --orphan flag. For example:

    git branch -m master old_master
    git checkout --orphan master
    

    Since the current branch now has no history, some commands may fail with errors like fatal: bad default revision 'HEAD' until after you make your first commit on the new master branch. This is normal, and is the same behavior you see in freshly-initialized repositories.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-23 14:27

    Here is something that is probably bad practice but works in a jam (building on top of aragaer's solution)

    git branch -D master
    git checkout --orphan master
    git rm -rf *
    

    After this, to submit a PR into this branch from a development branch currently being worked on without issue, do

    git checkout development-branch-name
    git rebase master
    git push -f development-branch-name
    

    Now you can submit a PR from development-branch-name into master without running into There isn't anything to compare. Nothing to compare, branches are entirely different commit histories issue on GitHub.

    I use this when I init a repo in a currently-underway project directory and want code review done via the GitHub UI on all the code in the project directory.

    0 讨论(0)
提交回复
热议问题