git 如何创建一个新的(和空的!)“根”分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15034390/
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 to create a new (and empty!) "root" branch?
提问by kjo
I would like to define a new "root" branch in this git repository. By "root" branch I mean a branch that is entirely independent of all the other branches in the repository1.
我想在这个 git 存储库中定义一个新的“根”分支。“根”分支是指完全独立于存储库1中所有其他分支的分支。
Unfortunately, even the commit (let's call it A
) at the very base of the repo's commit tree contains a lot of files (this was a repository that was initialized on an already fairly mature project).
不幸的是,即使是A
repo 提交树最基础的提交(我们称之为)也包含很多文件(这是一个在已经相当成熟的项目上初始化的存储库)。
This means that even if I gave A
as the new branch's <start-point>
, this new branch would not start from a "clean slate", but rather it would contain all the files that were committed in A
.
即使我给这意味着A
新科的<start-point>
,这个新分支将不会从一个“干净的石板”开始,而是它将包含所有已经提交的文件A
。
Is there some way I can create a completely bare branch in this repository, with <start-point>
as close to A
as possible?
有什么方法可以在这个存储库中创建一个完全裸露的分支,<start-point>
尽可能接近A
?
1BTW, this is notequivalent to creating a new repo. Separate repos would be less convenient for a lot of reasons.
1顺便说一句,这不等同于创建一个新的回购。由于很多原因,单独的存储库不太方便。
EDIT: OK, this is what I did, based on vcsjones' answer:
编辑:好的,这就是我所做的,基于vcsjones的回答:
# save rev of the current earliest commit
OLDBASE=$(git rev-list --max-parents=0 HEAD)
# create a new orphan branch and switch to it
git checkout --orphan newbranch
# make sure it's empty
git rm -rf .
# create a new empty commit in the new branch, and
# save its rev in NEWBASE
git commit --allow-empty -m 'base commit (empty)'
NEWBASE=$(git rev-list HEAD)
# specify $NEWBASE as the new parent for $OLDBASE, and
# run filter-branch on the original branch
echo "$OLDBASE $NEWBASE" > .git/info/grafts
git checkout master
git filter-branch
# NOTE: this assumes that the original repo had only one
# branch; if not, a git-filter-branch -f <branch> command
# need to be run for each additional branch.
rm .git/info/grafts
Although this procedure is a bit involved, the end result is an emptybase commit that can serve as the <start-point>
for any new "clean-slate branch"; all I'd need to do then is
虽然这个过程有点复杂,但最终的结果是一个空的基础提交,可以作为<start-point>
任何新的“clean-slate 分支”的提交;我需要做的就是
git checkout -b cleanslate $(git rev-list --max-parents=0 HEAD)
In the future I will always create new repositories like this:
将来,我将始终创建这样的新存储库:
git init
git commit --allow-empty -m 'base commit (empty)'
...so that the first commit is empty, and always available for starting a new independent branch. (This would be, I know, a very rarely needed facility, but it is quite effortless to make it readily available.)
...这样第一个提交是空的,并且始终可用于启动一个新的独立分支。(我知道,这将是一个很少需要的设施,但要使其随时可用是非常容易的。)
回答by vcsjones
Use the --orphan
when creating the branch:
--orphan
创建分支时使用:
git checkout --orphan YourBranchName
This will create a new branch with zero commits on it, however all of your files will be staged. At that point you could just remove them.
("remove them": A git reset --hard
will empty the index, leaving you with an empty working tree)
这将创建一个零提交的新分支,但是您的所有文件都将被暂存。那时你可以删除它们。
(“删除它们”:Agit reset --hard
将清空索引,留下一个空的工作树)
Take a look at the man pagefor checkout for more information on --orphan.
查看手册页以获取有关 --orphan 的更多信息。
回答by Mr_and_Mrs_D
To add to the accepted answer - best practiceto revert to clean state is to create an initial empty commitso you can easily rebase while setting up your branches for posterity. Plus since you want a clean state you probably have committed files that you shouldn't, so you have to remove them from the index. With those in mind you should:
要添加到已接受的答案中 -恢复到干净状态的最佳做法是创建一个初始的空提交,这样您就可以在为后代设置分支时轻松变基。另外,由于您想要一个干净的状态,您可能已经提交了您不应该提交的文件,因此您必须将它们从索引中删除。考虑到这些,您应该:
$ git checkout --orphan dev2
Switched to a new branch 'dev2'
$ git reset # unstage all the files, you probably don't want to commit all of them
$ git commit --allow-empty -m 'Initial empty commit'
[dev2 (root-commit) a515c28] Initial empty commit
回答by Captain Man
If you use git 2.23 or above you may be used to git switch
and git restore
instead of git checkout
. If so, the flag is the same as mentioned in this answer.
如果您使用 git 2.23 或更高版本,您可能习惯于使用git switch
和git restore
而不是git checkout
. 如果是这样,标志与此答案中提到的相同。
git switch --orphan YourBranchHere