151

When you do your first clone using the syntax

git clone username@server:gitRepo.git

Is it possible using your local repository to find the name of that initial clone?

(So in the above example, find gitRepo.git.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Toby
  • 8,483
  • 13
  • 45
  • 68
  • 1
    Related post - [How do you get the Git repository's name in some Git repository?](https://stackoverflow.com/q/15715825/465053) – RBT Jan 10 '20 at 10:16

9 Answers9

144
git config --get remote.origin.url
Straff
  • 5,499
  • 4
  • 33
  • 31
  • 2
    Agreed; this is best because it doesn't require a connection/auth to the git remote server. OP asked "using your local repository" but `git remote show origin` calls out to network. – Travis Wilson May 26 '17 at 19:40
  • 1
    This wrapped in a basename as [Casey' answer](https://stackoverflow.com/a/15118089/773623) works well – Jonathan Drapeau Jul 17 '19 at 17:20
104

In the repository root, the .git/config file holds all information about remote repositories and branches. In your example, you should look for something like:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = server:gitRepo.git

Also, the Git command git remote -v shows the remote repository name and URL. The "origin" remote repository usually corresponds to the original repository, from which the local copy was cloned.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
allait
  • 3,269
  • 3
  • 24
  • 9
  • 18
    You can also use `git remote show origin` to see much more information about just that remote. – Cascabel Nov 02 '10 at 14:38
  • 1
    Looks like these two methods are in the Answer are equivalent - it seems `git remote -v` just reads and writes `.git/config`. – flow2k May 20 '18 at 20:15
30

This is quick Bash command, that you're probably searching for, will print only a basename of the remote repository:

Where you fetch from:

basename $(git remote show -n origin | grep Fetch | cut -d: -f2-)

Alternatively where you push to:

basename $(git remote show -n origin | grep Push | cut -d: -f2-)

Especially the -n option makes the command much quicker.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Casey
  • 1,402
  • 1
  • 19
  • 23
17

I use this:

basename $(git remote get-url origin) .git

Which returns something like gitRepo. (Remove the .git at the end of the command to return something like gitRepo.git.)

(Note: It requires Git version 2.7.0 or later)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
adzenith
  • 757
  • 5
  • 8
3

I stumbled on this question trying to get the organization/repo string from a git host like github or gitlab.

This is working for me:

git config --get remote.origin.url | sed -e 's/^git@.*:\([[:graph:]]*\).git/\1/'

It uses sed to replace the output of the git config command with just the organization and repo name.

Something like github/scientist would be matched by the character class [[:graph:]] in the regular expression.

The \1 tells sed to replace everything with just the matched characters.

jhnstn
  • 2,438
  • 1
  • 18
  • 9
1

Powershell version of command for git repo name:

(git config --get remote.origin.url) -replace '.*/' -replace '.git'
Victor Fialkin
  • 165
  • 4
  • 7
0
git remote show origin -n | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'

It was tested with three different URL styles:

echo "Fetch URL: http://user@pass:gitservice.org:20080/owner/repo.git" | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'
echo "Fetch URL: Fetch URL: git@github.com:home1-oss/oss-build.git" | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'
echo "Fetch URL: https://github.com/owner/repo.git" | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
haolun
  • 304
  • 1
  • 9
0
git ls-remote --get-url | xargs basename         # gitRepo.git
git ls-remote --get-url | xargs basename -s .git # gitRepo

# zsh
git ls-remote --get-url | read
print $REPLY:t   # gitRepo.git
print $REPLY:t:r # gitRepo
Daniel Bayley
  • 1,493
  • 1
  • 10
  • 8
-1

Edited for clarity:

This will work to to get the value if the remote.origin.url is in the form protocol://auth_info@git_host:port/project/repo.git. If you find it doesn't work, adjust the -f5 option that is part of the first cut command.

For the example remote.origin.url of protocol://auth_info@git_host:port/project/repo.git the output created by the cut command would contain the following:

-f1: protocol: -f2: (blank) -f3: auth_info@git_host:port -f4: project -f5: repo.git

If you are having problems, look at the output of the git config --get remote.origin.url command to see which field contains the original repository. If the remote.origin.url does not contain the .git string then omit the pipe to the second cut command.

#!/usr/bin/env bash
repoSlug="$(git config --get remote.origin.url | cut -d/ -f5 | cut -d. -f1)"
echo ${repoSlug}
joehep
  • 136
  • 1
  • 5
  • It is possible that this will result in a blank string if the url for the remote repo is not in the form: `protocol://git@git_host:7999/project/repo.git` Adjust the **-f5** down in that case. – joehep Nov 04 '20 at 13:59