2010-11-02 53 views
67

克隆當您使用語法找出你從Git的

git clone [email protected]:gitRepo.git 

利用本地資源庫找到初始克隆的名稱是否有可能做你的第一個克隆原庫的名稱? (所以在上面的示例中找到gitRepo.git)

回答

64

在存儲庫根目錄中,.git/config文件包含有關遠程存儲庫和分支的所有信息。在您的例子中,你應該尋找類似:

[remote "origin"] 
    fetch = +refs/heads/*:refs/remotes/origin/* 
    url = server:gitRepo.git 

此外,git的命令:git remote -v顯示遠程倉庫名稱和網址。 「origin」遠程存儲庫通常對應於原始存儲庫,從中克隆了本地副本。

+13

您也可以使用'git的遠程顯示origin'看到更多的信息關於那遙控器。 – Cascabel 2010-11-02 14:38:59

26

這是你可能快速bash命令搜索
只打印遠程倉庫

,你取的基本名稱:
basename $(git remote show -n origin | grep Fetch | cut -d: -f2-)

或者,你推
basename $(git remote show -n origin | grep Push | cut -d: -f2-)

特別-n選項使命令更快

+0

第二個建議正是我想要的,謝謝。 – duma 2013-06-17 19:39:05

38
git config --get remote.origin.url 
+3

這應該是被接受的答案。 – 2017-05-12 15:41:35

+0

同意;這是最好的,因爲它不需要連接/授權給git遠程服務器。 OP詢問「使用你的本地存儲庫」,但是'git remote show origin'呼叫到網絡。 – 2017-05-26 19:40:40

0
git remote show origin -n | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil' 

3 URL風格測試:

echo "Fetch URL: http://[email protected]:gitservice.org:20080/owner/repo.git" | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil' 
echo "Fetch URL: Fetch URL: [email protected]:home1-oss/oss-build.git" | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil' 
echo "Fetch URL: https://github.com/owner/repo.git" | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil' 
+0

解釋你做了什麼,[如何回答](https://stackoverflow.com/help/how-to-answer) – Maher 2017-06-21 08:59:48

1

我用這個:

basename $(git remote get-url origin) .git

返回的東西像0一樣。 (在命令retun像gitRepo.git的末尾刪除.git

(注:需要的git> = 2.7.0)