How do you get the Git repository's name in some Git repository?

前端 未结 17 1196
囚心锁ツ
囚心锁ツ 2020-12-22 16:18

When you are working in some Git directory, how can you get the Git repository name in some Git repository? Are there any Git commands?

# I did check out bar         


        
相关标签:
17条回答
  • 2020-12-22 16:34

    Repo full name:

    git config --get remote.origin.url | grep -Po "(?<=git@github\.com:)(.*?)(?=.git)"
    
    0 讨论(0)
  • 2020-12-22 16:42

    If you are trying to get the username or organization name AND the project or repo name on github, I was able to write this command which works for me locally at least.

    ▶ 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
    

    This is the desired result as Vydia is the organization name and gourami is the package name. Combined they can help form the complete User/Repo path

    0 讨论(0)
  • 2020-12-22 16:42

    A little bit late for this question, but if you:

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

    You will see the url of the repository which will include the 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
    
    0 讨论(0)
  • 2020-12-22 16:43

    Here's mine:

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

    no better than the others, but I made it a bash function so I can drop in the remote name if it isn't 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
    }
    
    0 讨论(0)
  • 2020-12-22 16:45

    In general, you cannot do this. Git does not care how your git repository is named. For example, you can rename directory containing your repository (one with .git subdirectory), and git will not even notice it - everything will continue to work.

    However, if you cloned it, you can use command:

    git remote show origin
    

    to display a lot of information about original remote that you cloned your repository from, and it will contain original clone URL.

    If, however, you removed link to original remote using git remote rm origin, or if you created that repository using git init, such information is simply impossible to obtain - it does not exist anywhere.

    0 讨论(0)
  • 2020-12-22 16:46

    Other answers still won't work when the name of your directory does not correspond to remote repository name (and it could). You can get the real name of the repository with something like this:

    git remote show origin -n | grep "Fetch URL:" | sed -E "s#^.*/(.*)$#\1#" | sed "s#.git$##"

    Basically, you call git remote show origin, take the repository URL from "Fetch URL:" field, and regex it to get the portion with name: https://github.com/dragn/neat-vimrc.git

    0 讨论(0)
提交回复
热议问题