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

发布于 2024-09-29 21:17:26 字数 154 浏览 7 评论 0原文

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

git clone username@server:gitRepo.git

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

(因此在上面的示例中,找到 gitRepo.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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

如梦初醒的夏天 2024-10-06 21:17:26
git config --get remote.origin.url
git config --get remote.origin.url
海未深 2024-10-06 21:17:26

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

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

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

In the repository root, the .git/config file holds all information about remote repositories and branches. 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. The "origin" remote repository usually corresponds to the original repository, from which the local copy was cloned.

GRAY°灰色天空 2024-10-06 21:17: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< /strong> 选项使命令更快。

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

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.

心意如水 2024-10-06 21:17:26

我使用这个:

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

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

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

I use this:

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

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

(Note: It requires Git version 2.7.0 or later)

伤感在游骋 2024-10-06 21:17:26

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

这对我有用:

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

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

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

\1 告诉 sed 将所有内容替换为匹配的字符。

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

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.

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

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

给妤﹃绝世温柔 2024-10-06 21:17:26

git 存储库名称命令的 Powershell 版本:

(git config --get remote.origin.url) -replace '.*/' -replace '.git'

Powershell version of command for git repo name:

(git config --get remote.origin.url) -replace '.*/' -replace '.git'
百变从容 2024-10-06 21:17:26
git remote show origin -n | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'

它使用三种不同的 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'
git remote show origin -n | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'

It was tested with three different URL styles:

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'
夜访吸血鬼 2024-10-06 21:17:26
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
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
谎言 2024-10-06 21:17:26

为了清楚起见进行了编辑:

如果 remote.origin.url 的格式为 protocol://auth_info@git_host:port/project/repo.git,这将有助于获取值强>。如果您发现它不起作用,请调整第一个剪切命令中的 -f5 选项。

对于 protocol://auth_info@git_host:port/project/repo.gitremote.origin.url 示例,由 cut 创建的输出> 命令将包含以下内容:

-f1:协议:
-f2:(空白)
-f3: auth_info@git_host:端口
-f4:项目
-f5: repo.git

如果遇到问题,请查看 git config --get remote.origin.url 命令的输出,看看哪个字段包含原始存储库。如果 remote.origin.url 不包含 .git 字符串,则省略第二个 cut 命令的管道。

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

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. If you find it doesn't work, adjust the -f5 option that is part of the first cut command.

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:

-f1: protocol:
-f2: (blank)
-f3: auth_info@git_host:port
-f4: project
-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. If the remote.origin.url does not contain the .git string then omit the pipe to the second cut command.

#!/usr/bin/env bash
repoSlug="$(git config --get remote.origin.url | cut -d/ -f5 | cut -d. -f1)"
echo ${repoSlug}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文