Reputation: 2392
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.
Upvotes: 30
Views: 54786
Reputation: 13
If you have cloned the project from a remote, you can get its url:
git config --get remote.origin.url
Upvotes: 0
Reputation: 5900
basename $(git config remote.origin.url |sed "s/\.git$//")
or:
git config remote.origin.url |sed 's#.*\/\(.*\)\.git#\1#'
Upvotes: 1
Reputation: 66213
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'
Upvotes: 4
Reputation: 1021
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.
Upvotes: 28
Reputation: 1135
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.
Upvotes: 5
Reputation: 130
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.
Upvotes: 2
Reputation: 4698
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.
Upvotes: 8
Reputation: 129526
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.
Upvotes: 0
Reputation: 301037
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.
Upvotes: 1
Reputation: 6671
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.
Upvotes: 13
Reputation: 9003
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.
Upvotes: 5