简体   繁体   English

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

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

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 .) (所以在上面的例子中,找到gitRepo.git 。)

git config --get remote.origin.url

In the repository root, the .git/config file holds all information about remote repositories and branches.在存储库根目录中, .git/config文件包含有关远程存储库和分支的所有信息。 In your example, you should look for something like:在您的示例中,您应该查找以下内容:

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

Also, the Git command git remote -v shows the remote repository name and URL.此外,Git 命令git remote -v显示远程存储库名称和 URL。 The "origin" remote repository usually corresponds to the original repository, from which the local copy was cloned. “原始”远程存储库通常对应于原始存储库,从该存储库克隆了本地副本。

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 -n option makes the command much quicker.特别是-n选项使命令更快。

I use this:我用这个:

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

Which returns something like gitRepo .它返回类似gitRepo东西。 (Remove the .git at the end of the command to return something like gitRepo.git .) (删除命令末尾的.git以返回类似gitRepo.git 。)

(Note: It requires Git version 2.7.0 or later) (注:需要 Git 2.7.0 或更新版本)

I stumbled on this question trying to get the organization/repo string from a git host like github or gitlab.我偶然发现了这个问题,试图从 github 或 gitlab 等 git 主机获取organization/repo字符串。

This is working for me:这对我有用:

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

It uses sed to replace the output of the git config command with just the organization and repo name.它使用sedgit config命令的输出替换为组织和存储库名称。

Something like github/scientist would be matched by the character class [[:graph:]] in the regular expression.github/scientist这样的东西会被正则表达式中的字符类[[:graph:]]匹配。

The \\1 tells sed to replace everything with just the matched characters. \\1告诉 sed 只用匹配的字符替换所有内容。

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: git@github.com: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'

git repo 名称的 Powershell 版本命令:

(git config --get remote.origin.url) -replace '.*/' -replace '.git'
git ls-remote --get-url | xargs basename         # gitRepo.git
git ls-remote --get-url | xargs basename -s .git # gitRepo

# zsh
git ls-remote --get-url | read
print $REPLY:t   # gitRepo.git
print $REPLY:t:r # gitRepo

Edited for clarity:为清楚起见编辑:

This will work to to get the value if the remote.origin.url is in the form protocol://auth_info@git_host:port/project/repo.git .如果remote.origin.url的格式为protocol://auth_info@git_host:port/project/repo.git ,这将用于获取值。 If you find it doesn't work, adjust the -f5 option that is part of the first cut command.如果您发现它不起作用,请调整作为第一个 cut 命令一部分的-f5选项。

For the example remote.origin.url of protocol://auth_info@git_host:port/project/repo.git the output created by the cut command would contain the following:用于协议的示例remote.origin.url:// auth_info @ git_host:端口/项目/ repo.git切口命令创建将包含以下输出:

-f1: protocol: -f2: (blank) -f3: auth_info@git_host:port -f4: project -f5: repo.git -f1: 协议: -f2: (空白) -f3: auth_info@git_host:port -f4: 项目 -f5: repo.git

If you are having problems, look at the output of the git config --get remote.origin.url command to see which field contains the original repository.如果遇到问题,请查看git config --get remote.origin.url命令的输出以查看哪个字段包含原始存储库。 If the remote.origin.url does not contain the .git string then omit the pipe to the second cut command.如果remote.origin.url不包含.git字符串,则省略第二个cut命令的管道。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM