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

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

    Here's a bash function that will print the repository name (if it has been properly set up):

    __get_reponame ()
    {
        local gitdir=$(git rev-parse --git-dir)
    
        if [ $(cat ${gitdir}/description) != "Unnamed repository; edit this file 'description' to name the repository." ]; then
            cat ${gitdir}/description
        else
            echo "Unnamed repository!"
        fi
    }
    

    Explanation:

    local gitdir=$(git rev-parse --git-dir)
    

    This executes git rev-parse --git-dir, which prints the full path to the .git directory of the currrent repository. It stores the path in $gitdir.

    if [ $(cat ${gitdir}/description) != "..." ]; then
    

    This executes cat ${gitdir}/description, which prints the contents of the .git/description of your current repository. If you've properly named your repository, it will print a name. Otherwise, it will print Unnamed repository; edit this file 'description' to name the repository.

    cat ${gitdir}/description
    

    If the repo was properly named, then print the contents.

    else
    

    Otherwise...

    echo "Unnamed repository!"
    

    Tell the user that the repo was unnamed.


    Something similar is implemented in this script.

提交回复
热议问题