IT story

로컬 자식 분기가 있는지 확인하는 더 좋은 방법이 있습니까?

hot-time 2020. 5. 29. 23:44
반응형

로컬 자식 분기가 있는지 확인하는 더 좋은 방법이 있습니까?


다음 명령을 사용하여 로컬 git 브랜치 branch-name가 내 저장소에 있는지 확인합니다. 이 올바른지? 더 좋은 방법이 있습니까?

스크립트 내 에서이 작업을 수행하고 있습니다. 이러한 이유로 가능한 경우 배관 명령사용 하고 싶습니다 .

git show-ref --verify --quiet refs/heads/<branch-name>
# $? == 0 means local branch with <branch-name> exists. 

내가 아는 한, 그것은 스크립트에서 그것을하는 가장 좋은 방법입니다. 나는 그것에 더 많은 것이 있는지 확신 할 수 없지만 "그 명령이 당신이 원하는 모든 것을 수행합니다"라는 답이있을 수 있습니다 :)

주의해야 할 유일한 것은 브랜치 이름에 놀라운 문자가있을 수 있으므로 인용 할 수 있다는 것 <branch-name>입니다.


검색 엔진에서 '지점이 있는지 확인하십시오'를 검색하면이 페이지가 처음입니다.

원하는 것을 얻었지만 원래 게시물이 2011 년 이후로 업데이트 된 답변을 제공하고 싶습니다.

git rev-parse --verify <branch_name>

이것은 기본적으로 허용되는 답변과 동일하지만 "refs / heads /"를 입력 할 필요는 없습니다.


거의 다 왔어.

그냥 밖으로 떠날 --verify--quiet와 그렇지 않은 경우는 어느 지점이있는 경우 해시 또는 아무것도를 얻을.

변수에 지정하고 빈 문자열을 확인하십시오.

exists=`git show-ref refs/heads/<branch-name>`
if [ -n "$exists" ]; then
    echo 'branch exists!'
fi

나는 당신이 git show-branch여기서 사용할 수 있다고 생각합니다 .

$ git show-branch --list
  [master] test
* [testbranch] test
$ git show-branch testbranch
[testbranch] test
$ echo $?
0
$ git show-branch nonexistantbranch
fatal: bad sha1 reference nonexistantbranch
$ echo $?
128

그래서, $? == 0은 브랜치가 존재 함을 나타내며 심판 / 헤드 /의 배관을 전혀 파헤칠 필요가 없습니다. -rshow-branch에 전달하지 않는 한 로컬 브랜치에서만 작동합니다.


나는 추천한다 git show-ref --quiet refs/heads/$name.

  • --quiet 출력이 없다는 것을 의미합니다. 종료 상태를 깨끗하게 확인할 수 있기 때문에 좋습니다.

  • refs/heads/$name현지 지사로 제한하고 전체 이름과 일치 (그렇지 않으면 dev일치합니다 develop)

스크립트에서의 사용법 :

if git show-ref --quiet refs/heads/develop; then
    echo develop branch exists
fi

스크립트에서 사용하기 위해 :

git show-ref -q --heads <branch-name>

로컬 브랜치로 존재하는 0경우에만 종료 됩니다 <branch-name>.

예:

if git show-ref -q --heads <branch-name>; then
   echo 'Branch exists'
fi

Windows 배치 스크립트에서는 약간 다릅니다.

git rev-parse --verify <branch>

if %ERRORLEVEL% == 0  (
    echo "Yes"
) else (
    echo "No"
)

호출 해 봅시다 git is_localbranch(에 별칭을 추가해야합니다 .gitconfig).

용법:

$ git is_localbranch BRANCH

출처:

git branch | grep -w $1 > /dev/null
if [ $? = 0 ]
then
  echo "branch exists"
fi

초기 질문의 '업데이트'에 대한 '제안 편집'에 대한 검토 결과는 '댓글 또는 답변으로 작성되어야합니다'이므로 여기에 게시합니다.

다른 방법 가지 있지만 같은 이름을 가진 모든 참조 확인합니다뿐만 아니라 제안 @jhuynh을 .

git rev-parse --verify <reference-name>
# $? == 0 means reference with <reference-name> exists.

Issue with an 'Update' on initial quiestion explained:

Lets assume and check that 'master.000' is only a tag, such local branch does not exist, grep returns one entry wchich is a tag. Still rev-parse will return 0 if reference exists, even if such local branch does not exist. This is a false match, exactly as mentioned by @paul-s

$ git show-ref |grep master.000

f0686b8c16401be87e72f9466083d29295b86f4a refs/tags/master.000
$ git rev-parse --verify master.000
f0686b8c16401be87e72f9466083d29295b86f4a
$ echo $?
0

I wanted to use it the browser, so I made a little app that lets you check the validity of your branch name. It's backed by git, so you know what you're getting.

https://branch-checker.herokuapp.com/validate?branch=not//valid


yup there is one. git rev-parse [] …​ https://git-scm.com/docs/git-rev-parse Where u can find the set of arguments and the function.

        git rev-parse --verify <branch-name>

Neither git show-ref nor git rev-parse works on my case.

$ git --version
git version 2.21.0

$ git show-branch --list
* [master] mybranch commit

$ BRANCH_NAME=mybranch
$ git rev-parse --verify $BRANCH_NAME
fatal: Needed a single revision

$ git show-ref refs/heads/$BRANCH_NAME
<no otput>
$ [ $? == 0 ] && echo "$BRANCH_NAME exists" || echo "$BRANCH_NAME not exists"
mybranch not exists

I ended up with this

$ BRANCH_NAME=mybranch
$ SHOW_ALL=`git show-branch --all | grep -w $BRANCH_NAME`
$ [ $? == 0 ] && echo "$BRANCH_NAME exists" || echo "$BRANCH_NAME not exists"
mybranch exists

You can do also with a script file

#!/bin/sh
BRANCH_NAME=mybranch
if grep -Fqe $BRANCH_NAME << EOF
`git show-branch --all`
EOF
then
   echo "$BRANCH_NAME exists"
else
   echo "$BRANCH_NAME not exists"
fi

If you can manage to include grep.

git branch | grep -q <branch>

For use in a script, I recommend the following command:

git ls-remote --heads <repo_url> "<branch_name>" | wc -l

Note that <repo_url> can just be a "." to specify the local repo if you are inside its directory structure, the path to a local repo, or the address of a remote repo.

The command returns a 0 if the <branch_name> is not present of 1 if present.


$ git branch --list $branch_name | grep $branch_name then check the return value is 0 or 1.

참고URL : https://stackoverflow.com/questions/5167957/is-there-a-better-way-to-find-out-if-a-local-git-branch-exists

반응형