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

前端 未结 17 1197
囚心锁ツ
囚心锁ツ 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:47

    Also I just find that there is some repo information inside .git directory. So you can just watch FETCH_HEAD file in terminal to see repo's name:

    Example:

    cd your_project_folder/.git
    more FETCH_HEAD
    

    Output:

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

    And https://bitbucket.org/somebituser/some_repo.git is the name of your repository

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

    Well, if, for the repository name you mean the Git root directory name (the one that contains the .git directory) you can run this:

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

    The git rev-parse --show-toplevel part gives you the path to that directory and basename strips the first part of the path.

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

    I think this is a better way to unambiguously identify a clone of a repository.

    git config --get remote.origin.url and checking to make sure that the origin matches ssh://your/repo.

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

    You can use: git remote -v

    Documentation: https://git-scm.com/docs/git-remote

    Manage the set of repositories ("remotes") whose branches you track. -v --verbose Be a little more verbose and show remote url after name. NOTE: This must be placed between remote and subcommand.

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

    There's no need to contact the repository to get the name, and the folder name won't necessarily reflect the remote name.

    I've found this to be the most accurate and efficient way to get the current repository name:

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

    This should work as of Git 1.8.1.5. Prior to this, the now deprecated git-repo-config command would have worked (as early as Git 1.7.5).

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

    If you want the whole GitHub repository name ('full name') - user/repository, and you want to do it in with Ruby...

    git remote show origin -n | ruby -ne 'puts /^\s*Fetch.*:(.*).git/.match($_)[1] rescue nil'
    
    0 讨论(0)
提交回复
热议问题