2013-02-02 29 views
4

我如何獲得只有最終分支(不是任何其他提交的父項)的分支列表 - 其他分支是提交樹的提示(最終節點)。 我希望有一些選項 - 如git branch命令中的--final或--tips,但只有--merged,--no-merged和--contains選項。獲取最終分支的列表(最終提示)

+0

由於Git保存了提交的父項而不是子項,因此在歷史圖中找到葉節點(您所描述的「提示」)並不那麼簡單。編寫一個腳本來找出你想知道的內容會比較容易,但是它需要遍歷你的倉庫中的所有提交。 –

+0

我想找到可以被我刪除的分支。例如,如果我將刪除不是葉節點的分支技巧,我會爲我清理沒有用的分支。任何不是最終的分支都可以恢復,因爲它們的提交將在樹內。 – Perlover

回答

0

我想你想要的清單branch pointers。這基本上是分支結束時提交的提交列表。這些通常被稱爲masterHEAD,但可以是任何分支名稱。

例如

git branch -a 
1

由於分支是正義的移動當一個新提交被加入,同時它是在工作目錄主動標籤 - 所有分支「定義」樹的葉子節點。

但我想你是要求那些與另一個分支的葉子提交沒有共同祖先的分支。這樣您就可以在下面找到CE,但不是ABD

-A-o-B-o-C 
     \ 
     D-o-E 

如果是這樣的話,就有可能找到不共享一個共同的祖先是葉犯其他分支的所有分支的列表。例如,DE共享D當前葉落實的共同祖先。

這可能可以通過腳本來完成,但我不知道有一個git命令可以幫你完成所有分支。但git過去讓我感到吃驚,可以用它的(有時是隱含的)命令參數的組合來完成。


我做您的評論後,一些更多的思考,我覺得我更好地理解爲什麼你的要價是不會作爲一個命令是可能(或可能有用)。這是因爲像git merge --squash <branch>這樣的命令不會創建合併提交。創建的提交只是另一個常規提交 - 不包含任何來自另一個分支的提交。

例如,首先:

-A-o-B-o-C 
     \ 
     D-o-E 

,並在HEADC命令git merge --squash E將投入準備COMMITED索引包含在E的所有更改。一旦COMMITED樹看起來像

-A-o-B-o-o-C <== notice branch `C` has moved forward one commit 
     \ 
     D-o-E 

的承諾在C現在包含了所有從D-o-E什麼也沒有改變要說它是從哪裏來的。現在可能不需要D-o-E,但只有項目的知識才能告訴你。

隨着它明白你想要做的將是一個手動過程;我確實想到了一些可以幫助您確定是否可以刪除分支的命令。

這個git log命令產生一個只有提交裝飾(分支,標籤和HEAD)的圖形。

git log --all --graph --oneline --simplify-by-decoration --decorate 

運行這個命令會給你一個視覺效果,讓你快速看到'終點'分支在哪裏。提示:添加>> <filename>.txt將圖轉儲到可用鉛筆標記的文本文件。

此外,一旦確定了要保留的'end-point'分支,請運行以下命令查看<branch>的祖先分支列表。這是將完全合併的分支(通過合併提交)列入<branch>

git branch --merged <branch> 

這可能不是你想要的 - 但正如我上面所解釋的,我不認爲這是可能的。希望這些命令會有所幫助。

+0

是的,我想在你的例子中找到C&E分支,而不是A,B,D。 例如,我有一個有許多分支的存儲庫,我想清洗分支參考。 – Perlover

3

如果你得到所有裁判的名單,你可以列出所有不屬於另一個裁判的父裁判:

refs=`git show-ref -s` 
git log --oneline --decorate $refs --not `echo "$refs" | sed 's/$/^/'` 

這是通過過濾掉所有的父提交。

+0

我認爲這是行不通的:並非所有的refs在'refs'目錄中都有一個文件,其中一些文件位於'packed-refs'文件中。 – svick

+0

另外,出於某種原因,您的代碼根本不適用於我:它不返回任何內容。 – svick

+0

謝謝@svick,我用git替換了查找show-refs – AtnNn

3
git show -s --oneline --decorate $(
     git show-branch --independent -a 
) 

git show -s --format=%d $(
     git show-branch --independent -a 
) 

也許sed倒是品嚐。爲了打破所有refnames每行一個,並剝離,例如分組標點符號

git show -s --format=%d $(git show-branch --independent -a) \ 
| sed 's/,/\n/g; s/[()]//g' 
3

有一些有用的答案已經不足以表達什麼問:列表或分支機構(對我來說,這意味着分支名稱,不提交ID)。

獲取尖端分支名稱列表

{ 
     echo "** Below is list of tip branches a.k.a. final nodes." 
     echo "** These aren't reachable from any other branches." 
     echo "** Removing them is potentially DANGEROUS: it would make some commit unreachable." 
     TIPS=$(git rev-list --branches --children | grep -v ' ' | sort) 
     { while read branchname commitid message 
       do grep $commitid < <(echo $TIPS) -q && echo $branchname 
       done 
     } < <(git branch -v --no-abbrev | sed 's/\*//') 
     echo "** end of list" 
} 

結果是這樣的:

** Below is list of tip branches a.k.a. final nodes. 
** These aren't reachable from any other branches. 
** Removing them is potentially DANGEROUS: it would make some commit unreachable. 
feature-workingonit 
dev 
** end of list 

在整理東西,你可能需要的非尖端分支名稱獲取列表看到非提示分支。這是一個獲取它們列表的方法。唯一的變化是&&變爲||

{ 
     echo "** Below is list of non-tip branches a.k.a. inner nodes." 
     echo "** These are reachable from other branches." 
     echo "** If you remove them, all commits would stay reachable, but be careful anyway." 
     TIPS=$(git rev-list --branches --children | grep -v ' ' | sort) 
     { while read branchname commitid message 
       do grep $commitid < <(echo $TIPS) -q || echo $branchname 
       done 
     } < <(git branch -v --no-abbrev | sed 's/\*//') 
     echo "** end of list" 
} 

結果是這樣的:

** Below is list of non-tip branches a.k.a. inner nodes. 
** These are reachable from other branches. 
** If you remove them, all commits would stay reachable, but be careful anyway. 
feature-alreadymerged 
master 
** end of list 

獲取的非末端分支名稱清單,提交ID和消息

可以裝飾你的願望,例如通過改變:

echo $branchname 

分成

echo -e "$branchname\t$commitid\t$message" 

結果則是這樣的:

** Below is list of non-tip branches a.k.a. inner nodes. 
** These are reachable from other branches. 
** If you remove them, all commits would stay reachable, but be careful anyway. 
feature-alreadymerged 426ac5186841876163970afdcc5774d46641abc4 Cool feature foo, ref task #1234 
master 39148238924687239872eea425c0e837fafdcfd9 Some commit 
** end of list 

然後,您可以查看這些分支在你最喜歡的工具,看看它是否有意義將其刪除。