我想在這個git倉庫中定義一個新的「根」分支。通過「根」分支,我指的是完全獨立於存儲庫中的所有其他分支的分支。如何創建一個新的(和空的!)「根」分支?
不幸的是,即使是提交(我們稱之爲A
)在repo的提交樹的底部也包含很多文件(這是一個已經在相當成熟的項目上初始化的存儲庫)。
這意味着即使我將A
作爲新分支的<start-point>
,這個新分支不會從「乾淨的標準」開始,而是它將包含所有在A
中提交的文件。
是否有某種方法可以在此存儲庫中創建一個完全裸分支,<start-point>
儘可能接近A
?
順便說一句,這是等效於創建一個新的回購不。由於很多原因,單獨的回購將不太方便。
編輯:OK,這是我做的,基於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
雖然這個過程是參與了一下,最終的結果是空基本提交可以作爲<start-point>
用於任何新的「clean-slate分支」;所有我需要做的就是
git checkout -b cleanslate $(git rev-list --max-parents=0 HEAD)
今後我會一直創造新的存儲庫這樣的:
git init
git commit --allow-empty -m 'base commit (empty)'
......從而使第一承諾是空,始終可用於啓動一個新的獨立分支。 (這將是的,我知道,一個很少需要的設施,但它是相當費力,使其隨時可用。)
重複的http://stackoverflow.com/questions/13969050/how-to-create-a-new-empty-branch-for-a-new-project – Anonymoose 2013-11-04 04:52:22
@ Anonymoose:只是爲了記錄(而不是辯論):我不同意你的看法。 – kjo 2013-11-04 15:54:59
似乎有一個更簡單的解決方案基於'git rebase --onto',請參閱http://stackoverflow.com/questions/645450/insert-a-commit-before-the-root-commit-in-git – 2015-11-11 21:47:50