Finding out the name of the original repository you cloned from in Git

前端 未结 8 1072
予麋鹿
予麋鹿 2021-01-30 01:51

When you do your first clone using the syntax

git clone username@server:gitRepo.git

Is it possible using your local repository to find the name

相关标签:
8条回答
  • 2021-01-30 02:22

    I use this:

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

    Which returns something like gitRepo. (Remove the .git at the end of the command to return something like gitRepo.git.)

    (Note: It requires Git version 2.7.0 or later)

    0 讨论(0)
  • 2021-01-30 02:35

    Edited for clarity:

    This will work to to get the value if the remote.origin.url is in the form protocol://auth_info@git_host:port/project/repo.git. If you find it doesn't work, adjust the -f5 option that is part of the first cut command.

    For the example remote.origin.url of protocol://auth_info@git_host:port/project/repo.git the output created by the cut command would contain the following:

    -f1: protocol: -f2: (blank) -f3: auth_info@git_host:port -f4: project -f5: repo.git

    If you are having problems, look at the output of the git config --get remote.origin.url command to see which field contains the original repository. If the remote.origin.url does not contain the .git string then omit the pipe to the second cut command.

    #!/usr/bin/env bash
    repoSlug="$(git config --get remote.origin.url | cut -d/ -f5 | cut -d. -f1)"
    echo ${repoSlug}
    
    0 讨论(0)
  • 2021-01-30 02:36
    git config --get remote.origin.url
    
    0 讨论(0)
  • 2021-01-30 02:37

    This is quick Bash command, that you're probably searching for, will print only a basename of the remote repository:

    Where you fetch from:

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

    Alternatively where you push to:

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

    Especially the -n option makes the command much quicker.

    0 讨论(0)
  • 2021-01-30 02:37

    I stumbled on this question trying to get the organization/repo string from a git host like github or gitlab.

    This is working for me:

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

    It uses sed to replace the output of the git config command with just the organization and repo name.

    Something like github/scientist would be matched by the character class [[:graph:]] in the regular expression.

    The \1 tells sed to replace everything with just the matched characters.

    0 讨论(0)
  • 2021-01-30 02:37

    Powershell version of command for git repo name:

    (git config --get remote.origin.url) -replace '.*/' -replace '.git'
    
    0 讨论(0)
提交回复
热议问题