5
A
回答
2
我們很容易認爲master
總是master
而my_branch
總是my_branch
,但事實並非如此。假設你在Github,你的windows,你的linux和你的辦公室有你的倉庫。
你因此有8個不同的分支:
github/master
github/my_branch
windows/master
windows/my_branch
linux/master
linux/my_branch
office/master
office/my_branch
你作爲一個人看到他們爲master
和my_branch
但Git把它們作爲8個不同的分支。所以,如果你有這樣一個網絡:
------------------------------------------------ linux/master
\--/ \-------/ / \-------- office/my_branch
| \---|--\--------/-------------------\------- my_branch
| |
| office/master
|
windows/master
是什麼意思了問my_branch
從何而來?這是許多分支合併的結果!
在那裏,所以我想告訴你的是,有一個哲學問題,你的問題。然而,有一種方法可以回答它,儘管並不完美。首先讓我們來看看git log
:
git log my_branch --pretty=oneline --graph
爲您提供了合併和東西的一個漂亮的演示。從git-log手冊頁:
--first-parent
Follow only the first parent commit upon seeing a merge commit. This option can give a better overview when viewing the evolution of a particular topic branch,
because merges into a topic branch tend to be only about adjusting to updated upstream from time to time, and this option allows you to ignore the individual
commits brought in to your history by such a merge.
使用它可以得到分支的線性歷史記錄。卸下圖形和輸出只有SHA1s,您將獲得:
git log my_branch --pretty=format:"%H" --first-parent
使用下面的命令,你可以知道哪些分支包含一個SHA1:
git branch --contains <commit>
把腳本一起使用這些命令,你可以使用以下腳本,該腳本基本上可以找到另一個分支中包含的最新SHA1,而不是您感興趣的分支。然後輸出該分支。 (注:我不擅長在bash腳本還,所以這可能不是那麼有效):
#! /bin/bash
if [ $# -lt 1 ]; then
branch=master
else
branch=$1
fi
sha1s=$(git log $1 --pretty=format:"%H")
res="Doesn't branch from anything"
for i in $sha1s; do
b=$(git branch --contains $i | awk '{ if (NF > 1) print $2; else print $1 }') # use awk to remove * from current branch
other_branch="";
for j in $b; do
if [ $branch != $j ]; then
other_branch=$j
break;
fi
done
if [ -n "$other_branch" ]; then
res=$other_branch
break
fi
done
printf -- '%s\n' "$res"
我說這不是完美的,因爲下面的情況。想象一下,如果my_branch
從master
分支出來。事實上,你再看到這樣的圖形:
/------------ master
------(master)-----
\------------ my_branch
最初提交的含量爲都分支的歷史。不知道他們最初來自主人。因此,該腳本會告訴您my_branch
從master
分支,同時告訴您master
從my_branch
分支。沒有辦法確定哪一個是原始的。
相關問題
- 1. 開關哪個分支是git的一個分支
- 2. Git - 從某個分支創建分支
- 3. 我從哪個分支版本分支?
- 4. Git分支從當前分支的另一個分支
- 5. Git:保留多個分支檢出
- 6. 將git分支分成兩個分支?
- 7. GIT - 我從哪裏分支?
- 8. 從不同的檢出分支Git推分支
- 9. 的Git:如何列出這個分支,但沒有從合併分支機構
- 10. 在Git中檢出分支
- 11. 檢出遠程git分支?
- 12. 哪個標記或分支是從特定分支創建的
- 13. 查看分支最初是從哪個分支創建的?
- 14. 使用Git的多個分支有哪些分支策略?
- 15. 如何判斷哪個git分支被檢出?
- 16. 多個Git分支
- 17. git-svn如何知道dcommit要分支到哪個分支?
- 18. Git是如何從另一個分支
- 19. Git從一個分支複製到另一個分支
- 20. 這個提交哪個分支?
- 21. 哪個分支推到git rebase
- 22. 我如何知道從哪個git遠程分支我的本地分支被檢出?
- 23. 如何一個Git分支分成等2個分支
- 24. 使用Git的多個分支指出
- 25. 單個分支的Git導出
- 26. git svn基於git分支創建一個新的svn分支
- 27. Git:在檢出新分支之前沒有提交分支
- 28. Git - 創建一個新的分支並只提交給這個分支?
- 29. git分支(沒有分支)
- 30. Git - 將舊分支移動到另一個分支是什麼?
您的意思是它正在跟蹤的遠程分支,或分支分支分支(如果有的話)?我不認爲你總能找到後者。 –
這個分支(如果有的話)的本地分支。無法說明這一點比你做得更好。 – Dziamid
我不認爲Git甚至記錄了這些信息,儘管我可能會誤解。 –