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

前端 未结 11 1320
终归单人心
终归单人心 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:37

    There is no such thing as a project name in Git. You are simply getting the name of the folder the repository is located in remotely. Git has no built-in way of computing this as it has absolutely no use for it.

    0 讨论(0)
  • 2021-02-01 17:39
    basename $(git config remote.origin.url |sed "s/\.git$//")
    

    or:

    git config remote.origin.url |sed 's#.*\/\(.*\)\.git#\1#'
    
    0 讨论(0)
  • 2021-02-01 17:44

    I would like to point out the same answer @Mike mentioned in the comments to your question, the $GIT_DIR/description file. Some other software use this for the name of the repository, such as the post-receive-email hook script. (Which actually does sed -ne '1p' "$GIT_DIR/description") and thus simply use the first line from that file as the name of the repository.

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

    What you are doing is fine. I would not trust that someone won't change the name in a readme file. Use the URL as you are doing. Decide on a convention so that origin always refers to the central repo where the urls will contain the identity.

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

    The remote url or the folder in which the git repo is kept can be anything as far as Git is concerned. So you do not have a built-in way for checking this. Git cannot identify what the name of the project is. You have to ( or the owner of the project). Usually, this will be in the form of a README or some similar file checked into the repository that gives the name of the project.

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

    Chained head awk and sed calls like this

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

    can be combined into one sed call like this:

    git remote -v  | sed -rn '1s#.*/(.*)\.git.*#\1#p'
    
    0 讨论(0)
提交回复
热议问题