2012-03-08 95 views
3

我想知道是否有尋找,如果提交的是一個家長向另一種廣義的方法。的Git包含任意之間提交

git branch --contains <hash> 

幾乎是我想要的。它列出了包含提交的所有分支,但我想知道任意提交是否「包含」另一個提交。

我臨時黑客是創建一個分支,在提交,然後檢查其是否包含在列表中,但這似乎邋遢。

git branch __temp_branch__ <hash1> 
git branch --contains <hash2> # check if __temp_branch__ is in output 
git branch -d __temp_branch__ 

回答

2

如果commitA是commitB的祖先,然後git merge-base commitA commitBcommitA。例如:

if [ "$(git merge-base $commitA $commitB)" = "$commitA" ]; then ... 

這實質上比grepping的git rev-list輸出更高效,應就此給你。

+0

我打算接受這個答案,因爲它比其他答案快得多,並且確保兩個哈希都是有效的。 – 2012-03-08 22:02:28

2

git rev-list <childSHA> | grep <parentSHA>

這將返回<parentSHA>,如果它是的<childSHA>

+0

很好,謝謝:) – 2012-03-08 20:08:43

0

一種可能性父母也許是:

git log --ancestry-path <parentSHA> <childSHA> 

如果返回呢,你知道parentSHA不家長。