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.
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).
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
.
Is there some way I can create a completely bare branch in this repository, with <start-point>
as close to A
as possible?
1BTW, this is not equivalent to creating a new repo. Separate repos would be less convenient for a lot of reasons.
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
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.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…