如何在某些 Git 存储库中获取 Git 存储库的名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15715825/
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 15:52:44  来源:igfitidea点击:

How do you get the Git repository's name in some Git repository?

gitrepository

提问by gipcompany

When you are working in some Git directory, how can you get the Git repository name in some Git repository? Are there any Git commands?

当你在某个 Git 目录下工作时,如何获取某个 Git 存储库中的 Git 存储库名称?有任何 Git 命令吗?

# I did check out bar repository and working in somewhere 
# under bar directory at this moment such as below.

$ git clone git://github.com/foo/bar.git
$ cd bar/baz/qux/quux/corge/grault # and I am working in here!
$ git xxx # <- ???
bar

回答by Fuad Saud

Well, if, for the repository name you mean the Git root directory name (the one that contains the .git directory) you can run this:

好吧,如果对于存储库名称,您指的是 Git 根目录名称(包含 .git 目录的名称),您可以运行以下命令:

basename `git rev-parse --show-toplevel`

The git rev-parse --show-toplevelpart gives you the path to that directory and basenamestrips the first part of the path.

git rev-parse --show-toplevel部分为您提供该目录的路径并basename删除路径的第一部分。

回答by mvp

In general, you cannot do this. Git does not care how your git repository is named. For example, you can rename directory containing your repository (one with .gitsubdirectory), and git will not even notice it - everything will continue to work.

一般来说,你不能这样做。Git 并不关心你的 git 仓库是如何命名的。例如,您可以重命名包含您的存储库的目录(一个带有.git子目录),而 git 甚至不会注意到它 - 一切都会继续工作。

However, if you cloned it, you can use command:

但是,如果您克隆它,则可以使用命令:

git remote show origin

to display a lot of information about original remote that you cloned your repository from, and it will contain original clone URL.

显示有关您从中克隆存储库的原始远程的大量信息,它将包含原始克隆 URL。

If, however, you removed link to original remote using git remote rm origin, or if you created that repository using git init, such information is simply impossible to obtain - it does not exist anywhere.

但是,git remote rm origin如果您使用 删除了指向原始远程的链接,或者如果您使用 来创建该存储库git init,则根本无法获得此类信息 - 它不存在于任何地方。

回答by TastyWheat

There's no need to contact the repository to get the name, and the folder name won't necessarily reflect the remote name.

不需要联系存储库来获取名称,文件夹名称不一定反映远程名称。

I've found this to be the most accurate and efficient way to get the current repository name:

我发现这是获取当前存储库名称的最准确和有效的方法:

basename -s .git `git config --get remote.origin.url`

This should work as of Git 1.8.1.5. Prior to this, the now deprecated git-repo-configcommand would have worked (as early as Git 1.7.5).

这应该从 Git 1.8.1.5 开始工作。在此之前,现在已弃用的git-repo-config命令会起作用(早在 Git 1.7.5 中)。

回答by vulcan raven

In git v2.7.0+, a subcommand get-urlwas introduced to git-remotecommand.

git v2.7.0+ 中get-urlgit-remotecommand引入了一个子命令。

POSIX shell:

POSIX 外壳:

basename $(git remote get-url origin)

PowerShell:

电源外壳:

Split-Path -Leaf (git remote get-url origin)

回答by dragn

Other answers still won't work when the name of your directory does not correspond to remote repository name (and it could). You can get the realname of the repository with something like this:

当您的目录名称与远程存储库名称不对应(并且可以)时,其他答案仍然无效。您可以使用以下内容获取存储库的真实名称:

git remote show origin -n | grep "Fetch URL:" | sed -E "s#^.*/(.*)$#\1#" | sed "s#.git$##"

git remote show origin -n | grep "Fetch URL:" | sed -E "s#^.*/(.*)$#\1#" | sed "s#.git$##"

Basically, you call git remote show origin, take the repository URL from "Fetch URL:" field, and regex it to get the portion with name: https://github.com/dragn/neat-vimrc.git

基本上,你打电话git remote show origin,从拿库的URL“获取URL:”字段,正则表达式,它获得与名称的部分: https://github.com/dragn/整齐-的vimrc的.git

回答by OLIVER.KOO

I think this is a better way to unambiguously identify a cloneof a repository.

我认为这是明确识别存储库克隆的更好方法。

git config --get remote.origin.urland checking to make sure that the origin matches ssh://your/repo.

git config --get remote.origin.url并检查以确保来源匹配ssh://your/repo

回答by jonnyjandles

A little bit late for this question, but if you:

这个问题有点晚了,但如果你:

cat /path/to/repo/.git/config

You will see the url of the repository which will include the reponame:

您将看到存储库的 url,其中将包含 reponame:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://github.com/your_git_user_name/your_git_repo_name.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

回答by animatedgif

If you are trying to get the username or organization name AND the project or repo name on github, I was able to write this command which works for me locally at least.

如果您尝试在 github 上获取用户名或组织名称以及项目或存储库名称,我可以编写此命令,该命令至少在本地对我有用。

? git config --get remote.origin.url
# => https://github.com/Vydia/gourami.git

? git config --get remote.origin.url | sed 's/.*\/\([^ ]*\/[^.]*\).*//' # Capture last 2 path segments before the dot in .git
# => Vydia/gourami

This is the desired result as Vydiais the organization name and gouramiis the package name. Combined they can help form the complete User/Repopath

这是所需的结果,Vydia组织名称gourami也是包名称。结合它们可以帮助形成完整的User/Repo路径

回答by Lemonada

Repo full name:

回购全名:

git config --get remote.origin.url | grep -Po "(?<=git@github\.com:)(.*?)(?=.git)"

回答by MD XF

Here's a bash function that will print the repository name (if it has been properly set up):

这是一个 bash 函数,它将打印存储库名称(如果已正确设置):

__get_reponame ()
{
    local gitdir=$(git rev-parse --git-dir)

    if [ $(cat ${gitdir}/description) != "Unnamed repository; edit this file 'description' to name the repository." ]; then
        cat ${gitdir}/description
    else
        echo "Unnamed repository!"
    fi
}

Explanation:

解释:

local gitdir=$(git rev-parse --git-dir)

This executes git rev-parse --git-dir, which prints the full path to the .gitdirectory of the currrent repository. It stores the path in $gitdir.

这会执行git rev-parse --git-dir,它会打印当前.git存储库目录的完整路径。它将路径存储在$gitdir.

if [ $(cat ${gitdir}/description) != "..." ]; then

This executes cat ${gitdir}/description, which prints the contents of the .git/descriptionof your current repository. If you've properly named your repository, it will print a name. Otherwise, it will print Unnamed repository; edit this file 'description' to name the repository.

这会执行cat ${gitdir}/description,它会打印.git/description当前存储库的内容。如果您正确命名了您的存储库,它将打印一个名称。否则会打印Unnamed repository; edit this file 'description' to name the repository.

cat ${gitdir}/description

If the repo was properly named, then print the contents.

如果 repo 被正确命名,则打印内容。

else

Otherwise...

除此以外...

echo "Unnamed repository!"

Tell the user that the repo was unnamed.

告诉用户该 repo 未命名。



Something similar is implemented in this script.

这个脚本中实现了类似的东西。