当您在某个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 ls-remote --get-url | xargs basename -s .git # bar

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

其他回答

回购全称:

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

这种使用git-remote的方法对我来说很有效,适用于HTTPS遥控器:

$ git remote -v | grep "(fetch)" | sed 's/.*\/\([^ ]*\)\/.*/\1/'
                                                |  |        | |
                                                |  |        | +---------------+
                                                |  |        | Extract capture |
                                                |  +--------------------+-----+
                                                |Repository name capture|
                                                +-----------------------+

例子

好吧,如果你指的是Git根目录名(包含.git目录的根目录名),你可以这样运行:

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

git rev-parse——show-toplevel部分提供了该目录的路径,basename删除了路径的第一部分。

您可以将项目名称存储在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-2.18.2中运行得非常好,并且可以从git目标库外部启动:

Basename -s .git $(git——git-dir=/<项目路径>/.)Git远程获取url来源)