如何创建一个新的(并且是空的!)"root"分支? [英] How to create a new (and empty!) "root" branch?
问题描述
我想在这个git仓库中定义一个新的"root"分支. 根"分支是指完全独立于存储库 1 中所有其他分支的分支.
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.
不幸的是,即使在回购的提交树的最底层的提交(我们称其为A
)也包含很多文件(这是一个已经相当成熟的项目上初始化的存储库).
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
作为新分支的<start-point>
,该新分支也不会从干净的板岩"开始,而是包含了所有在A
中提交的文件.
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
.
我是否可以通过某种方式在此存储库中创建一个完全裸露的分支,使<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?
1 BTW,这不等同于创建新的存储库.出于许多原因,单独的存储库可能不太方便.
1BTW, this is not equivalent to creating a new repo. Separate repos would be less convenient for a lot of reasons.
编辑:好的,这是我根据 vcsjones '答案所做的:
EDIT: OK, this is what I did, based on vcsjones' answer:
# 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
尽管此过程涉及到一点点,但最终结果是一个 空 基本提交,该提交可以用作任何新的"clean-slate分支"的<start-point>
;然后我要做的就是
Although this procedure is a bit involved, the end result is an empty base commit that can serve as the <start-point>
for any new "clean-slate branch"; all I'd need to do then is
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.)
推荐答案
在创建分支时使用--orphan
:
git checkout --orphan YourBranchName
这将创建一个零提交的新分支,但是所有文件都将被暂存.此时,您可以将其删除.
(删除它们":git reset --hard
将清空索引,而留下一棵空的工作树)
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)
查看手册页以了解更多信息有关--orphan的信息.
Take a look at the man page for checkout for more information on --orphan.
这篇关于如何创建一个新的(并且是空的!)"root"分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!