Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

git - How to create a new (and empty!) "root" branch?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the --orphan when creating the branch:

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)

Take a look at the man page for checkout for more information on --orphan.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...