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
)在存储库提交树的最底部包含许多文件(这是在一个已经相当成熟的项目上初始化的存储库)。
这意味着即使我给了A
作为新分行的<start-point>
,这个新分支不会从“干净的石板”开始,而是包含在A
.
有什么方法可以在这个存储库中创建一个完全裸露的分支,<start-point>
尽可能接近A
尽可能?
1BTW, this is not equivalent to creating a new repo. Separate repos would be less convenient for a lot of reasons.
EDIT: 好的,这就是我所做的,基于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
虽然这个过程有点复杂,但最终的结果是empty基础提交可以作为<start-point>
对于任何新的“全新分支”;那么我需要做的就是
git checkout -b cleanslate $(git rev-list --max-parents=0 HEAD)
将来我将始终创建这样的新存储库:
git init
git commit --allow-empty -m 'base commit (empty)'
...这样第一次提交是empty,并且始终可以启动新的独立分支。 (我知道,这将是一个很少需要的设施,但要使其随时可用是相当容易的。)