Is there a git command that returns the current project name?

前端 未结 11 1321
终归单人心
终归单人心 2021-02-01 17:31

Does git have a built-in command for showing the name of the current remote project? Right now I\'m using this:

git remote -v | head -n1 | awk \'{print $2}\' | s         


        
相关标签:
11条回答
  • 2021-02-01 17:56

    The command git remote -v can not be assumed as reliable because your repository can work with more than one remote repositories. For example, your project is your-project and you have added another-project. After the command you are expecting to see the name of your project but you'll see the name of another project:

    $ git remote -v | head -n1
    ABC https://git.team1.ourcompany.com/another-project.git (fetch)
    ABC https://git.team1.ourcompany.com/another-project.git (push)
    origin https://git.ourcompany.com/your-project.git (fetch)
    origin https://git.ourcompany.com/your-project.git (push)
    

    What I could suggest is to check your repository's configuration, for example:

    $ git config --local remote.origin.url
    https://git.ourcompany.com/your-project.git
    

    In the first approach this is the more reliable but doesn't give 100% insurance.

    0 讨论(0)
  • 2021-02-01 17:57
    git remote get-url origin | xargs basename -s .git
    
    0 讨论(0)
  • 2021-02-01 17:59

    It looks like your script is pulling the last part of the remote URL and using that as the project name. This works when using a single remote site, such as http://bitbucket.org but your system will not work universally across all users of that repository.

    Users generally all have different remote repositories, in fact on many projects you will have multiple remotes. Git does not care where a repository comes from when fetching and merging. The merge algorithm will even accept branches with no common ancestor.

    The only real solution is to create a text file in the root of each repository that contains the project name. This solution will work across all users, regardless of how they setup their remotes.

    0 讨论(0)
  • 2021-02-01 17:59

    I was looking for same information in order to customize my shell prompt, so I decided to give a try and ended up with this command which output just the name of the project:

        $ git config --local remote.origin.url|sed -n 's#.*/\([^.]*\)\.git#\1#p'
    

    It should works in any case if your remote origin url is SSH, HTTPS with DNS or IP based.

    • SSH based
      • dns : ssh://git@github.com:user/project.git
      • ip : ssh://git@192.30.252.130:user/project.git
    • HTTPS based
      • dns : https://github.com/user/project.git
      • ip : https://192.30.252.130/user/project.git

    If you don't have remote configured, only a local repository and your top level folder is the name of the project you can use git rev-parse and basename inside your git tree (not reliable solution). It will output the project name:

     TOP=$(git rev-parse --show-toplevel); echo ${TOP##*/}
    

    NB: GH doesn't allow you to clone using IP directly on HTTPS because certificate chain validation. It was just to illustrate the use cases.

    0 讨论(0)
  • 2021-02-01 17:59

    For remote folder name it'd be better

    git remote -v | head -n1 | awk '{print $2}' | sed -e 's,.*:\(.*/\)\?,,' -e 's/\.git$//'
    

    because it could be git@something.com:repo_name - without any slash.

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