找出您在 Git 中克隆的原始存储库的名称
当您使用语法进行第一次克隆时,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在存储库根目录中,
.git/config
文件保存有关远程存储库和分支的所有信息。在您的示例中,您应该查找类似以下内容:此外,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: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.这是您可能正在寻找的快速 Bash 命令,
将仅打印远程存储库的基本名称:
您从哪里获取:
或者您推送到哪里:
尤其是
-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:
Alternatively where you push to:
Especially the
-n
option makes the command much quicker.我使用这个:
它返回类似 gitRepo 的内容。 (删除命令末尾的
.git
以返回类似gitRepo.git
的内容。)(注意:需要 Git 版本 2.7.0 或更高版本)
I use this:
Which returns something like
gitRepo
. (Remove the.git
at the end of the command to return something likegitRepo.git
.)(Note: It requires Git version 2.7.0 or later)
我在尝试从 github 或 gitlab 等 git 主机获取
organization/repo
字符串时偶然发现了这个问题。这对我有用:
它使用 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:
It uses
sed
to replace the output of thegit 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.git 存储库名称命令的 Powershell 版本:
Powershell version of command for git repo name:
它使用三种不同的 URL 样式进行了测试:
It was tested with three different URL styles:
为了清楚起见进行了编辑:
如果 remote.origin.url 的格式为 protocol://auth_info@git_host:port/project/repo.git,这将有助于获取值强>。如果您发现它不起作用,请调整第一个剪切命令中的 -f5 选项。
对于 protocol://auth_info@git_host:port/project/repo.git 的 remote.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 命令的管道。
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.