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

当前回答

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

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

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

这应该在Git 1.8.1.5起作用。在此之前,现在已弃用的Git -repo-config命令可以工作(早在Git 1.7.5时)。

其他回答

回购全称:

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

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

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

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

这应该在Git 1.8.1.5起作用。在此之前,现在已弃用的Git -repo-config命令可以工作(早在Git 1.7.5时)。

它在git-2.18.2中运行得非常好,并且可以从git目标库外部启动:

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

如果你试图获得用户名或组织名称和github上的项目或回购名称,我能够编写这个命令,至少在本地为我工作。

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

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

这是理想的结果,因为Vydia是组织名称,而gourami是包名称。它们组合在一起可以帮助形成完整的用户/回购路径

这是我的:

git remote --verbose | grep origin | grep fetch | cut -f2 | cut -d' ' -f1

没有比其他的更好,但我把它作为一个bash函数,所以如果它不是origin,我可以在远程名称中删除。

grurl () {
  xx_remote=$1
  [ -z "$xx_remote" ] && xx_remote=origin
  git remote --verbose | grep "$1" | grep fetch | cut -f2 | cut -d' ' -f1
  unset xx_remote
}