代码之家  ›  专栏  ›  技术社区  ›  Toby

找出您在Git中克隆的原始存储库的名称

  •  95
  • Toby  · 技术社区  · 14 年前

    当您使用语法进行第一次克隆时

    git clone username@server:gitRepo.git
    

    (在上面的例子中,找到 gitRepo.git .)

    5 回复  |  直到 5 年前
        1
  •  86
  •   Peter Mortensen icecrime    5 年前

    在存储库根目录中 .git/config

    [remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = server:gitRepo.git
    

    另外,Git命令 git remote -v

        2
  •  98
  •   Straff    8 年前
    git config --get remote.origin.url
    
        3
  •  28
  •   Peter Mortensen icecrime    5 年前

    这是快速Bash命令,你可能正在搜索, 将只打印远程存储库的基名称:

    :

    basename $(git remote show -n origin | grep Fetch | cut -d: -f2-)
    

    或者你推的地方 :

    basename $(git remote show -n origin | grep Push | cut -d: -f2-)
    

    -n 选项使命令更快。

        4
  •  10
  •   Peter Mortensen icecrime    5 年前

    我用这个:

    basename $(git remote get-url origin) .git
    

    它返回的结果是 gitRepo . (移除 .git 在命令结束时返回 gitRepo.git .)

    (注意:它需要Git版本2.7.0或更高版本)

        5
  •  0
  •   Peter Mortensen icecrime    5 年前
    git remote show origin -n | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'
    

    它使用三种不同的URL样式进行测试:

    echo "Fetch URL: http://user@pass:gitservice.org:20080/owner/repo.git" | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'
    echo "Fetch URL: Fetch URL: git@github.com:home1-oss/oss-build.git" | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'
    echo "Fetch URL: https://github.com/owner/repo.git" | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'
    
        6
  •  0
  •   jhnstn    5 年前

    organization/repo 来自git主机(如github或gitlab)的字符串。

    这对我有用:

    git config --get remote.origin.url | sed -e 's/^git@.*:\([[:graph:]]*\).get/\1/'
    

    sed 替换 git config

    有点像 github/scientist 将由字符类匹配 [[:graph:]] 在正则表达式中。

    这个 \1 告诉sed用匹配的字符替换所有内容。