IT story

일부 Git 저장소에서 Git 저장소의 이름을 어떻게 얻습니까?

hot-time 2020. 7. 28. 07:52
반응형

일부 Git 저장소에서 Git 저장소의 이름을 어떻게 얻습니까?


Git 디렉토리에서 작업 할 때 Git 저장소에서 Git 저장소 이름을 어떻게 얻을 수 있습니까? 힘내 명령이 있습니까?

# 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

저장소 이름에 대해 Git 루트 디렉토리 이름 (.git 디렉토리가 포함 된 이름)을 의미하는 경우 다음을 실행할 수 있습니다.

basename `git rev-parse --show-toplevel`

git rev-parse --show-toplevel부분은 해당 디렉토리의 경로를 제공하고 경로 basename의 첫 부분을 제거합니다.


일반적으로이 작업을 수행 할 수 없습니다. Git은 git 저장소의 이름을 지정하지 않습니다. 예를 들어, 저장소 ( .git하위 디렉토리가 있는 디렉토리)가 포함 된 디렉토리의 이름을 바꿀 수 있으며 git은 디렉토리를 알지 못합니다. 모든 것이 계속 작동합니다.

그러나 복제 한 경우 다음 명령을 사용할 수 있습니다.

git remote show origin

리포지토리를 복제 한 원본 원격에 대한 많은 정보를 표시하려면 원본 복제 URL이 포함됩니다.

그러나을 사용하여 원래 원격지에 대한 링크를 제거 git remote rm origin했거나을 사용하여 해당 리포지토리를 만든 경우 git init이러한 정보를 얻는 것이 불가능합니다.이 정보는 어디에도 존재하지 않습니다.


이름을 얻기 위해 저장소에 연결할 필요가 없으며 폴더 이름에 원격 이름이 반드시 반영되는 것은 아닙니다.

이것이 현재 저장소 이름을 얻는 가장 정확하고 효율적인 방법이라는 것을 알았습니다.

basename -s .git `git config --get remote.origin.url`

이것은 Git 1.8.1.5부터 작동합니다. 이 전에는 더 이상 사용되지 않는 git-repo-config명령이 작동했습니다 (Git 1.7.5부터).


에서 자식 v2.7.0의 + , 부속 명령이 get-url소개되었다 git-remote명령.

POSIX 쉘 :

basename $(git remote get-url origin)

PowerShell :

Split-Path -Leaf (git remote get-url origin)

디렉토리 이름이 원격 리포지토리 이름과 일치하지 않는 경우에도 다른 답변이 작동하지 않습니다. 다음과 같이 저장소 실제 이름을 얻을 수 있습니다 .

git remote show origin -n | grep "Fetch URL:" | sed -E "s#^.*/(.*)$#\1#" | sed "s#.git$##"

기본적으로을 호출 git remote show origin하고 "Fetch URL :"필드에서 리포지토리 URL을 가져 와서 정규식으로 이름을 가진 부분을 가져옵니다 : https://github.com/dragn/ neat-vimrc .git


이것이 리포지토리 의 복제본명확하게 식별하는 더 좋은 방법이라고 생각 합니다.

git config --get remote.origin.url원점이 일치하는지 확인합니다 ssh://your/repo.


이 질문에 조금 늦었지만 다음과 같은 경우 :

cat /path/to/repo/.git/config

reponame을 포함 할 저장소의 URL이 표시됩니다.

[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

저장소 이름을 올바르게 설정하는 bash 함수는 다음과 같습니다 (제대로 설정 한 경우).

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


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.


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'

Repo full name:

git config --get remote.origin.url | grep -Po "(?<=git@github\.com:)(.*?)(?=.git)"

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


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
}

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


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.

참고URL : https://stackoverflow.com/questions/15715825/how-do-you-get-the-git-repositorys-name-in-some-git-repository

반응형