2013-04-10 42 views
4

假設我們已經下工作拷貝結構:如何找出一個工作副本包含一個子樹?

. 
├── adm 
└── etc 

$ git remote -v 
origin [email protected]:xxx/my.git (fetch) 
origin [email protected]:xxx/my.git (push) 

現在,讓我們假設我們已經通過git subtree添加了一分項目:

git remote add extlip [email protected]:yyy/ExtLib.git 
git subtree add -P tech -m "added extlib as a sub-project" extlib/master 

這樣的

. 
├── adm 
├── etc 
└── tech 

$ git remote -v 
origin [email protected]:xxx/my.git (fetch) 
origin [email protected]:xxx/my.git (push) 
extlip [email protected]:yyy/ExtLib.git (fetch) 
extlip [email protected]:yyy/ExtLib.git (push) 

現在,假設你是不是在做這個項目了一段時間,你怎麼確定子項目的根源在哪裏?說,你如何確定你的「子樹」的位置,哪一個是正確的遠程?或者,你怎麼確定你是「子樹」呢?

回答

2

在添加子樹時檢測提交的一種方法是查找合併提交,其中兩個父代不屬於同一棵樹,或換句話說,這兩個提交不共享任何先前的提交。

如何在bash從根您的Git倉庫中運行它檢測到這樣一個示例腳本:

#!/bin/bash 

# To separate the rev-list by newlines 
IFS=$'\n' 

# This rev list will return the hash and the parents, separated by spaces, 
# of any merge commit found in the history of HEAD 
for hashes in $(git rev-list --merges --parents HEAD); do 

    # To split the commits by space 
    IFS=$' ' 
    hashList=($hashes) 

    # Merge base will find the most recent commit shared by all the 
    # given commits, output dumped just to not clutter 
    git merge-base ${hashList[1]} ${hashList[2]} > /dev/null 

    # We care only if such commit did not exist, which means each parent is 
    # in its own separate tree 
    if [[ $? != 0 ]]; then 
     echo "Subtree merge: ${hashList[0]}" 
    fi 
done 
unset IFS 
+0

首先,感謝您的回覆!是的,用這個腳本你可以回答這個問題:「當前的工作副本是否包含任何子樹?」但與git的表達消息相比,這並不方便。除此之外,我想知道子樹的根在哪裏,遠程是什麼,......這樣的東西。但是,你的腳本是一個開始。再次感謝! /納米 – 2013-04-17 16:56:25

相關問題