programing

Git에서 복제한 원본 저장소 이름 찾기

cafebook 2023. 9. 25. 23:05
반응형

Git에서 복제한 원본 저장소 이름 찾기

구문을 사용하여 첫 번째 복제를 수행할 때

git clone username@server:gitRepo.git

로컬 저장소를 사용하여 해당 초기 클론의 이름을 찾을 수 있습니까?

(그러므로 위의 예에서 다음을 찾습니다.gitRepo.git.)

git config --get remote.origin.url

저장소 루트에서,.git/config파일에는 원격 리포지토리 및 분기에 대한 모든 정보가 저장됩니다.예제에서는 다음과 같은 것을 찾아야 합니다.

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

또한 Git 명령어는git remote -v는 원격 저장소 이름과 URL을 보여 줍니다."origin" 원격 저장소는 대개 로컬 복사본이 복제된 원본 저장소에 해당합니다.

검색 중인 quick Bash 명령어는 원격 저장소의 기본 이름만 인쇄합니다.

가져온 위치:

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

또는 푸시할 위치:

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

특히 옵션을 사용하면 명령이 훨씬 빨라집니다.

나는 이것을(를)

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

그 말은 다음과 같은 것을 돌려줍니다.gitRepo. (제거).git명령의 끝에 다음과 같은 것을 돌려주는 것.gitRepo.git.)

(참고: Git 버전 2.7.0 이상 필요)

나는 이 질문을 우연히 얻으려다organization/repo깃허브나 깃랩 같은 깃 호스트의 문자열입니다.

이것이 저에게 효과가 있습니다.

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

사용합니다.sed의 출력을 대체합니다.git config조직 및 레포 이름만으로 명령을 수행할 수 있습니다.

뭐 이런 거.github/scientist캐릭터 클래스와 일치할 것입니다.[[:graph:]]정규 표현으로

\1모든 것을 일치된 문자로만 바꾸라고 sed에게 말합니다.

git repo 이름에 대한 powershell 버전의 명령:

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

테스트 대상은 다음 세 가지 URL 스타일입니다.

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'
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

명확하게 하기 위해 편집됨:

remote.origin.urlprotocol://auth_info@git_host:port/project/repo.git 형식인 경우 값을 가져옵니다.작동하지 않을 경우 첫 번째 컷 명령의 일부인 -f5 옵션을 조정합니다.

protocol://auth_info@git_host:port/project/repo.gitremote.origin.url 예제의 경우 cut 명령에 의해 생성된 출력에는 다음이 포함됩니다.

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

문제가 있는 경우 출력을 살펴봅니다.git config --get remote.origin.url원래 리포지토리를 포함하는 필드를 확인하는 명령입니다.remote.origin.url .git 문자열이 없는 경우 두 번째 cut 명령에 파이프를 생략합니다.

#!/usr/bin/env bash
repoSlug="$(git config --get remote.origin.url | cut -d/ -f5 | cut -d. -f1)"
echo ${repoSlug}

언급URL : https://stackoverflow.com/questions/4076239/finding-out-the-name-of-the-original-repository-you-cloned-from-in-git

반응형