找出您在 Git 中克隆的原始存储库的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4076239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:23:23  来源:igfitidea点击:

Finding out the name of the original repository you cloned from in Git

gitversion-controlconfiguration-files

提问by Toby

When you do your first clone using the syntax

当您使用语法进行第一次克隆时

git clone username@server:gitRepo.git

Is it possible using your local repository to find the name of that initial clone?

是否可以使用您的本地存储库来查找该初始克隆的名称?

(So in the above example, find gitRepo.git.)

(所以在上面的例子中, find gitRepo.git。)

采纳答案by allait

In the repository root, the .git/configfile holds all information about remote repositories and branches. In your example, you should look for something like:

在存储库根目录中,该.git/config文件包含有关远程存储库和分支的所有信息。在您的示例中,您应该查找以下内容:

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

Also, the Git command git remote -vshows the remote repository name and URL. The "origin" remote repository usually corresponds to the original repository, from which the local copy was cloned.

此外,Git 命令git remote -v显示远程存储库名称和 URL。“原始”远程存储库通常对应于原始存储库,从该存储库克隆了本地副本。

回答by Straff

git config --get remote.origin.url

回答by Casey

This is quick Bash command, that you're probably searching for, will print only a basename of the remote repository:

这是您可能正在搜索的快速 Bash 命令,它只会打印远程存储库的基本名称:

Where you fetch from:

哪里获取:

basename $(git remote show -n origin | grep Fetch | cut -d: -f2-)

Alternatively where you push to:

或者你推

basename $(git remote show -n origin | grep Push | cut -d: -f2-)

Especially the -noption makes the command much quicker.

特别是该-n选项使命令更快。

回答by adzenith

I use this:

我用这个:

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

Which returns something like gitRepo. (Remove the .gitat the end of the command to return something like gitRepo.git.)

返回类似gitRepo. (删除.git命令末尾的 以返回类似gitRepo.git. 的内容。)

(Note: It requires Git version 2.7.0 or later)

(注:需要 Git 2.7.0 或更新版本)

回答by jhnstn

I stumbled on this question trying to get the organization/repostring from a git host like github or gitlab.

我偶然发现了这个问题,试图organization/repo从 github 或 gitlab 等 git 主机获取字符串。

This is working for me:

这对我有用:

git config --get remote.origin.url | sed -e 's/^git@.*:\([[:graph:]]*\).git//'

It uses sedto replace the output of the git configcommand with just the organization and repo name.

它用于仅用组织和存储库名称sed替换git config命令的输出。

Something like github/scientistwould be matched by the character class [[:graph:]]in the regular expression.

github/scientist这样的东西会被[[:graph:]]正则表达式中的字符类匹配。

The \1tells sed to replace everything with just the matched characters.

\1告诉sed只用匹配的字符来代替一切。

回答by joehep

This will work to to get the value.

这将有助于获得价值。

#!/usr/bin/env bash
repoSlug="$(git config --get remote.origin.url | cut -d/ -f5 | cut -d. -f1)"
echo ${repoSlug}

回答by haolun

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

It was tested with three different URL styles:

它使用三种不同的 URL 样式进行了测试:

echo "Fetch URL: http://user@pass: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'