28

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}' | sed 's/.*\///' | sed 's/\.git//'

...but it seems like there would be a built-in equivalent.

5
  • 3
    What do you mean by "name of the project" Nov 18, 2011 at 23:26
  • I like your solution. ;-) Other than this, I think the only supported thing you could do would be to look at .git/description.
    – mpontillo
    Nov 18, 2011 at 23:42
  • What exactly are you trying to accomplish with this? Perhaps we can offer a better solution. Nov 19, 2011 at 1:40
  • I also like your solution. I agree about the multiple remotes issue, but for now it's nice :)
    – Noich
    Jul 25, 2013 at 6:41
  • @sbz wrote perfect solution for it below - just find it
    – fearis
    Mar 17, 2016 at 9:46

12 Answers 12

27

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.

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.

2
  • This is what I've looked for. Secure as basename can be diffrent than name of repo if you cloning with '.' -> clone repo-name . Im using it for adding remote servers in script ;) Cheers !
    – fearis
    Mar 17, 2016 at 9:44
  • 1
    i think the regex does not work if there is a dot in the repo-name like [email protected]:user/my-repo.com.git Edit: Maybe this way? 's#.*/\([^\/]*\)\.git#\1#p'
    – Riscie
    May 14, 2019 at 14:10
13

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.

1
  • Voting for this one for speaking to the remotes. Thanks
    – brock
    Nov 21, 2011 at 22:42
8

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 [email protected]:repo_name - without any slash.

1
  • For the format ssh://localhost/user/reponame (or ssh://github/user/reponame, if you have an insteadOf rule in your git config) the awk/sed statements don't quite work so you end up with empty output; git config --local remote.origin.url | tr '/' '\n' | tail -1 works well enough for my case, though it won't work for the format you describe, where there is no slash
    – adam
    Aug 2, 2022 at 13:58
5

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.

4

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.

4

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'
1
  • Wow, thanks. That is quite a bit faster too. While I generally prefer the first format, because for me it feels a little bit easier to read, I can't deny the speed improvements of your format. When I run the first one, I'm seeing head, awk, sed, and sed taking .025 seconds each, and your single call to sed is done in .005. Nice!
    – brock
    Mar 2, 2020 at 18:52
4
git remote get-url origin | xargs basename -s .git
2

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.

1

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.

1
basename $(git config remote.origin.url |sed "s/\.git$//")

or:

git config remote.origin.url |sed 's#.*\/\(.*\)\.git#\1#'
0

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.

3
  • .git/description is local to a repo. How will you get the description from remote, especially on a project hosted on github
    – manojlds
    Nov 18, 2011 at 23:59
  • you have to rely on something from the url to uniquely identify it. Nov 19, 2011 at 0:22
  • Um, that's the point of other answers here. What are you asking? I am questioning how .git/description helps the OP here.
    – manojlds
    Nov 19, 2011 at 0:43
0

If you have cloned the project from a remote, you can get its url:

git config --get remote.origin.url

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.