当您在某个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对象数据库中的对象中,并在带注释的标记中引用它。

例如,存储库名称“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

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

其他回答

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

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

你将看到存储库的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

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

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

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

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

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

git remote show origin

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

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

另外,我刚刚发现在.git目录中有一些回购信息。所以你可以在终端中查看FETCH_HEAD文件来查看repo的名称:

例子:

cd your_project_folder/.git
more FETCH_HEAD

输出:

672e38391557a192ab23a632d160ef37449c56ac        https://bitbucket.org/fonjeekay/some_repo

https://bitbucket.org/somebituser/some_repo.git是存储库的名称

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

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