![](/img/trans.png)
[英]How can I create a new git branch from the base of the commit tree , which has no commit in common with master branch ? for gh-pages
[英]In Git, how can I make a new commit on a branch with the tree equal to the tree from another commit
我有一種情況
...--x--y--z master
...--a--b--c someOtherBranch
我想在master分支上進行一次新提交,只需將文件樹更改為someOtherBranch的樹即可。 即,我想進入一個狀態
...--x--y--z--c1 master
...--a--b--c someOtherBranch
提交c1
的文件樹看起來像c
樹。 (這大概意味着它是完全相同的文件樹對象。)
我怎樣才能做到這一點?
我知道這會在z和c1之間造成潛在的大且不希望的不連續性。
尚不清楚為什么要這樣做(通常的原因在git merge -s ours
,這使得進行合並提交而不是單親提交),但是使用都很容易做到git merge --squash -s ours
使用git merge --squash -s ours
后面是git commit
或git commit-tree
。 編輯 : -s ours
from master將保留錯誤的樹( z
而不是c
)。 它可以利用這一點,但這是如果你想要一個合並提交。 請參閱以下部分。
(下面的所有命令均假定您現在是master
。)
要使用git commit-tree
,必須提供一些額外的參數,這需要更多的設置:
hash=$(git commit-tree -p HEAD -F /tmp/commit-msg someOtherBranch^{tree})
例如,提交消息存儲在文件/tmp/commit-msg
。 生成的哈希ID是一個新的提交對象,該對象尚未在任何分支上,但是現在很容易“快速合並”到:
git merge --ff-only $hash
或git reset
到:
git reset --hard $hash
正如John Szakmeister所評論的那樣 ,您還可以使用git checkout
將另一個提交的索引和工作樹覆蓋到當前的索引中。 如果您有一個名為README
的文件,而又沒有README
,則應該首先對所有內容進行git rm
,例如,只有README.md
。
或者您可以使用git read-tree
來做到這一點,我認為這更簡單:
git read-tree -m -u someOtherBranch; git commit
(這里不需要^{tree}
,因為git read-tree
會自己將提交解析為樹)。
在使用git merge -s ours
設置真正的合並之后,也可以使用read-tree(或remove-and-checkout)技巧。 那是:
git merge -s ours someOtherBranch
git read-tree -m -u someOtherBranch
git commit
將准備保留z
的合並,然后用c
的索引和工作樹替換索引和工作樹,然后使樹c
與c
而不是z
匹配的新提交c1
:
...--x--y--z--c1 <-- master
/
...--a--b---c <-- someOtherBranch
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.