如何完全清空 Git 中的主分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15454555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I completely empty the master branch in Git?
提问by Marty Wallace
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.
我想完全清空 Git 中的主分支。现在,我还想保留从 master 分支出来的所有其他分支。
Is this possible and how?
这是可能的吗?
回答by aragaer
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.
但实际上,您可以简单地将当前存储库保存到其他位置并创建一个新存储库。
回答by Todd A. Jacobs
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:
首先,您需要移动或删除当前的 master 分支。就个人而言,我更喜欢将它移到一边而不是删除它。之后,您只需使用--orphan
标志创建一个没有父级的新分支。例如:
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.
由于当前分支现在没有历史记录,因此某些命令可能会失败并出现错误,例如fatal: bad default revision 'HEAD'
直到您在新的主分支上进行第一次提交之后。这是正常现象,与您在新初始化的存储库中看到的行为相同。
回答by Spencer MacBeth
Here is something that is probably bad practice but works in a jam (building on top of aragaer's solution)
这可能是不好的做法,但在果酱中起作用(建立在 aragaer 的解决方案之上)
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
在此之后,要从当前正在处理的开发分支向此分支提交 PR 没有问题,请执行
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.
现在,您可以从development-branch-name
into提交 PR,master
而不会There isn't anything to compare. Nothing to compare, branches are entirely different commit histories
在 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.
当我在当前正在进行的项目目录中初始化一个 repo 并希望通过 GitHub UI 对项目目录中的所有代码进行代码时,我会使用它。
回答by Tomá? Fejfar
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:
其他答案建议创建一个新的主分支(就像在空存储库中一样)。但是没有历史的大师不是一个好习惯。这使得使用 rebase 变得困难,因为您永远无法对第一次提交进行 rebase。任何存储库上 master 的第一次提交都应该是空的。对此有一个低级管道解决方案:
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
最后你将 master 重置为那个提交
$ git reset --hard a5c0737b92e5e5d4502e15b93d7a46d1e17b2b22
回答by Sylvain Defresne
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.
您可以尝试将 master 分支重置为第一个提交,git checkout master; git reset --hard ${sha-of-first-commit}
然后修改第一个提交以删除其中的文件。
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.
这应该会给你一个原始的 master 分支,并保持所有其他分支不变,但是由于你正在重写历史,所有克隆你的存储库都需要修复。
回答by Shibbir Ahmed
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?
您是否删除了主分支或分支本身的内容?