Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

github

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.

like image 270
brock Avatar asked Nov 18 '11 23:11

brock


People also ask

What is the command to get the current status of the git repository?

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git. Status output does not show you any information regarding the committed project history.

How do I pull a project in git bash?

From your repository page on GitHub, click the green button labeled Clone or download, and in the “Clone with HTTPs” section, copy the URL for your repository. Next, on your local machine, open your bash shell and change your current working directory to the location where you would like to clone your repository.


2 Answers

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
  • 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.

like image 155
sbz Avatar answered Sep 22 '22 06:09

sbz


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.

like image 23
Jacob Groundwater Avatar answered Sep 22 '22 06:09

Jacob Groundwater