当您在某个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

当前回答

在git v2.7.0+中,为git-remote command引入了一个子命令get-url。

POSIX壳:

basename $(git remote get-url origin)

PowerShell:

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

其他回答

下面是一个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
}

解释:

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

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

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

这将执行cat ${gitdir}/description,打印当前存储库的.git/description的内容。如果您正确地命名了存储库,它将打印一个名称。否则,它将打印未命名的存储库;编辑这个文件'description'来命名存储库。

cat ${gitdir}/description

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

else

否则……

echo "Unnamed repository!"

告诉用户回购是未命名的。


这个脚本中实现了类似的功能。

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

但是,如果你克隆了它,你可以使用命令:

git remote show origin

来显示关于您从其克隆存储库的原始远程的大量信息,并且它将包含原始克隆URL。

但是,如果您使用git remote rm origin删除了到原始远程的链接,或者如果您使用git init创建了该存储库,则这些信息根本不可能获得——它不存在于任何地方。

当目录名称与远程存储库名称不对应时,其他答案仍然不起作用(它可以)。 你可以通过这样的方式获取存储库的真实名称:

git远程显示起源- n | grep获取URL: | sed - e” #^.*/(.*)$#\ 1 #“| sed”# . # #美元”

基本上,你调用git remote show origin,从“Fetch URL:”字段中获取存储库URL,并正则化它以获得带有名称的部分: https://github.com/dragn/neat-vimrc.git

您可以将项目名称存储在git对象数据库中的对象中,并在带注释的标记中引用它。

例如,存储库名称“GnuMake”就是标记名称“nameRepo”。ssh -F指定ssh配置文件khnSrv_al注册为远端主机

命令:在git数据库中创建并放置一个对象,在带注释的标记中获取并保存到放置对象的链接。

git -C /path/dir_git -c user.email=you@example.com -c user.name=ВашеИмя tag -a nameRepo -m 'Имя репозитория' $(echo 'GnuMake'|git -C /path/dir_git hash-object -w --stdin)

al@rznCad:~$ git -C ~/experiments/git -c user.email=you@example.com -c user.name=ВашеИмя tag -a nameRepo -m 'Имя репозитория' $(echo 'GnuMake'|git -C ~/experiments/git hash-object -w --stdin)

al@rznCad:~$ ssh -F /ubData/docs/PktDstSSH/master_config khnSrv_al "git -C /home/al/newRepos/GNU_Make -c user.email=you@example.com -c user.name=ВашеИмя tag -a nameRepo -m 'Имя репозитория' \$(echo 'GnuMake'|git -C /home/al/newRepos/GNU_Make hash-object -w --stdin)"

命令:使用名为"nameRepo"的标记从git数据库对象中提取存储库名称

git -C /path/dir_git cat-file blob nameRepo

al@rznCad:~$ git -C ~/experiments/git cat-file blob nameRepo
=>GnuMake

al@rznCad:~$ ssh -F /ubData/docs/PktDstSSH/master_config khnSrv_al "git -C /home/al/newRepos/GNU_Make cat-file blob nameRepo"
=>GnuMake

al@rznCad:~$ ssh -F /ubData/docs/PktDstSSH/master_config khnSrv_al git -C /home/al/newRepos/GNU_Make show nameRepo
tag nameRepo
Tagger: ВашеИмя <you@example.com>
Date:   Wed Jan 25 09:01:18 2023 +0200

Имя репозитория
GnuMake

注意,在存储库中不需要有其他任何东西,您可以在任何时候创建一个存储库名称

git ls-remote --get-url | xargs basename -s .git # bar

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