Reputation: 5183
When you are working in some Git directory, how can you get the Git repository name in some Git repository? Are there any Git commands?
# I did check out bar repository and working in somewhere
# under bar directory at this moment such as below.
$ git clone git://github.com/foo/bar.git
$ cd bar/baz/qux/quux/corge/grault # and I am working in here!
$ git xxx # <- ???
bar
Upvotes: 283
Views: 465844
Reputation: 60235
To get the last url you fetched from, you can awk '{print $NF;exit}' .git/FETCH_HEAD
; to get the name from that use basename
as in @TastyWheat's answer so
basename $(awk '{print $NF;exit}' .git/FETCH_HEAD) .git
If you might be cloning from paths with embedded whitespace you can use
basename "$(awk -F/ '{print $NF;exit}' .git/FETCH_HEAD)" .git
Upvotes: 0
Reputation: 31
If the repo name may have more than 1 folder level then "basename" isn't enough.
I find the following the simplest to cover all cases:
git remote get-url origin | sed 's/^.*://;s/.git$//'
This command will produce a single line with the repo url wich can easilly be filtered with sed to extract just the name from it.
Upvotes: 1
Reputation: 229
you might be after 'origin'
you can use git remote -v
the first word is the repository name which is commonly named as 'origin'
Upvotes: 3
Reputation: 3366
In Git, there's no concept of a repository name. The repository itself is kept under a directory in the file system (the one that contains the .git
directory) and you can get the name of that directory with the following command:
basename `git rev-parse --show-toplevel`
The git rev-parse --show-toplevel
part gives you the path to that directory and basename
strips the first part of the path.
Another common sense interpretation to a "Git's repository name" is the name used to identify the repository in a Git hosting service (such as GitHub or GitLab). That name can be obtained by looking at the remote information, and a few different ways to achieve that are documented in other answers to this question.
Upvotes: 285
Reputation: 399
You can store the project name in an object in the git object database and reference it in the annotated tag.
For example, the repository name "GnuMake" is the tag name "nameRepo". ssh -F specifies the ssh configuration file khnSrv_al is registered as a remote host
Command: Create and place an object in a git database, get and save a link to the placed object in the annotated tag.
git -C /path/dir_git -c [email protected] -c user.name=ВашеИмя tag -a nameRepo -m 'Имя репозитория' $(echo 'GnuMake'|git -C /path/dir_git hash-object -w --stdin)
al@rznCad:~$ git -C ~/experiments/git -c [email protected] -c user.name=ВашеИмя tag -a nameRepo -m 'Имя репозитория' $(echo 'GnuMake'|git -C ~/experiments/git hash-object -w --stdin)
al@rznCad:~$ ssh -F /ubData/docs/PktDstSSH/master_config khnSrv_al "git -C /home/al/newRepos/GNU_Make -c [email protected] -c user.name=ВашеИмя tag -a nameRepo -m 'Имя репозитория' \$(echo 'GnuMake'|git -C /home/al/newRepos/GNU_Make hash-object -w --stdin)"
Command: Extract the repository name from a git database object using a tag named "nameRepo"
git -C /path/dir_git cat-file blob nameRepo
al@rznCad:~$ git -C ~/experiments/git cat-file blob nameRepo
=>GnuMake
al@rznCad:~$ ssh -F /ubData/docs/PktDstSSH/master_config khnSrv_al "git -C /home/al/newRepos/GNU_Make cat-file blob nameRepo"
=>GnuMake
al@rznCad:~$ ssh -F /ubData/docs/PktDstSSH/master_config khnSrv_al git -C /home/al/newRepos/GNU_Make show nameRepo
tag nameRepo
Tagger: ВашеИмя <[email protected]>
Date: Wed Jan 25 09:01:18 2023 +0200
Имя репозитория
GnuMake
Note that it is not necessary to have anything else in the repository other than this and you can create a repository name at any time
Upvotes: 0
Reputation: 1570
git ls-remote --get-url | xargs basename -s .git # bar
# zsh
git ls-remote --get-url | read
print $REPLY:t:r # bar
Upvotes: 2
Reputation: 3185
This one works pretty well with git-2.18.2 and can be launched from outside git target repository:
basename -s .git $(git --git-dir=/<project-path>/.git remote get-url origin)
Upvotes: 1
Reputation: 1109
If you are trying to get the username or organization name AND the project or repo name on github, I was able to write this command which works for me locally at least.
▶ git config --get remote.origin.url
# => https://github.com/Vydia/gourami.git
▶ git config --get remote.origin.url | sed 's/.*\/\([^ ]*\/[^.]*\).*/\1/' # Capture last 2 path segments before the dot in .git
# => Vydia/gourami
This is the desired result as Vydia
is the organization name and gourami
is the package name. Combined they can help form the complete User/Repo
path
Upvotes: 31
Reputation: 41
Repo full name:
git config --get remote.origin.url | grep -Po "(?<=git@github\.com:)(.*?)(?=.git)"
Upvotes: 4
Reputation: 6505
A little bit late for this question, but if you:
cat /path/to/repo/.git/config
You will see the url of the repository which will include the reponame:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/your_git_user_name/your_git_repo_name.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
Upvotes: 8
Reputation: 33562
In git v2.7.0+, a subcommand get-url
was introduced to git-remote
command.
POSIX shell:
basename $(git remote get-url origin)
PowerShell:
Split-Path -Leaf (git remote get-url origin)
Upvotes: 53
Reputation: 474
Also I just find that there is some repo information inside .git
directory. So you can just watch FETCH_HEAD
file in terminal to see repo's name:
Example:
cd your_project_folder/.git
more FETCH_HEAD
Output:
672e38391557a192ab23a632d160ef37449c56ac https://bitbucket.org/fonjeekay/some_repo
And https://bitbucket.org/somebituser/some_repo.git
is the name of your repository
Upvotes: 0
Reputation: 116048
In general, you cannot do this. Git does not care how your git repository is named. For example, you can rename directory containing your repository (one with .git
subdirectory), and git will not even notice it - everything will continue to work.
However, if you cloned it, you can use command:
git remote show origin
to display a lot of information about original remote that you cloned your repository from, and it will contain original clone URL.
If, however, you removed link to original remote using git remote rm origin
, or if you created that repository using git init
, such information is simply impossible to obtain - it does not exist anywhere.
Upvotes: 216
Reputation: 2317
There's no need to contact the repository to get the name, and the folder name won't necessarily reflect the remote name.
I've found this to be the most accurate and efficient way to get the current repository name:
basename -s .git `git config --get remote.origin.url`
This should work as of Git 1.8.1.5. Prior to this, the now deprecated git-repo-config
command would have worked (as early as Git 1.7.5).
Upvotes: 192
Reputation: 1792
If you want the whole GitHub repository name ('full name') - user/repository, and you want to do it in with Ruby...
git remote show origin -n | ruby -ne 'puts /^\s*Fetch.*:(.*).git/.match($_)[1] rescue nil'
Upvotes: 2
Reputation: 51935
Unfortunately, it seems that Git has no such command built in. But you can easily add it yourself with Git aliases and some shell magic.
As pointed out by this answer, you can use git rev-parse --show-toplevel
to show the root of your current Git folder.
If you want to use this regularly, it's probably more convenient to define it as an alias. For this, used git config alias.root '!echo "$(git rev-parse --show-toplevel)"'
. After this, you can use git root
to see the root folder of the repository you're currently in.
If you want to use another subcommand name than root
, simply replace the second part of alias.root
in the above command with whatever you want.
For details on aliases in Git, see also the git config
man page.
Upvotes: 1
Reputation: 5993
I think this is a better way to unambiguously identify a clone of a repository.
git config --get remote.origin.url
and checking to make sure that the origin matches ssh://your/repo
.
Upvotes: 6
Reputation: 8129
Here's a bash function that will print the repository name (if it has been properly set up):
__get_reponame ()
{
local gitdir=$(git rev-parse --git-dir)
if [ $(cat ${gitdir}/description) != "Unnamed repository; edit this file 'description' to name the repository." ]; then
cat ${gitdir}/description
else
echo "Unnamed repository!"
fi
}
Explanation:
local gitdir=$(git rev-parse --git-dir)
This executes git rev-parse --git-dir
, which prints the full path to the .git
directory of the currrent repository. It stores the path in $gitdir
.
if [ $(cat ${gitdir}/description) != "..." ]; then
This executes cat ${gitdir}/description
, which prints the contents of the .git/description
of your current repository. If you've properly named your repository, it will print a name. Otherwise, it will print Unnamed repository; edit this file 'description' to name the repository.
cat ${gitdir}/description
If the repo was properly named, then print the contents.
else
Otherwise...
echo "Unnamed repository!"
Tell the user that the repo was unnamed.
Something similar is implemented in this script.
Upvotes: 1
Reputation: 35
You can use: git remote -v
Documentation: https://git-scm.com/docs/git-remote
Manage the set of repositories ("remotes") whose branches you track. -v --verbose Be a little more verbose and show remote url after name. NOTE: This must be placed between remote and subcommand.
Upvotes: 1
Reputation: 2218
Here's mine:
git remote --verbose | grep origin | grep fetch | cut -f2 | cut -d' ' -f1
no better than the others, but I made it a bash function so I can drop in the remote name if it isn't origin.
grurl () {
xx_remote=$1
[ -z "$xx_remote" ] && xx_remote=origin
git remote --verbose | grep "$1" | grep fetch | cut -f2 | cut -d' ' -f1
unset xx_remote
}
Upvotes: 0
Reputation: 16521
This approach using git-remote
worked well for me for HTTPS remotes:
$ git remote -v | grep "(fetch)" | sed 's/.*\/\([^ ]*\)\/.*/\1/'
| | | |
| | | +---------------+
| | | Extract capture |
| +--------------------+-----+
|Repository name capture|
+-----------------------+
Example
Upvotes: 2
Reputation: 1050
Other answers still won't work when the name of your directory does not correspond to remote repository name (and it could). You can get the real name of the repository with something like this:
git remote show origin -n | grep "Fetch URL:" | sed -E "s#^.*/(.*)$#\1#" | sed "s#.git$##"
Basically, you call git remote show origin
, take the repository URL from "Fetch URL:" field, and regex it to get the portion with name:
https://github.com/dragn/neat-vimrc.git
Upvotes: 21