지정된 원격 저장소에 원격 분기가 있는지 확인하는 방법은 무엇입니까?
주어진 원격 저장소에있는 경우 특정 분기에 대해 하위 트리 병합을 수행해야합니다. 문제는 원격 저장소가 로컬로 체크 아웃되지 않았기 때문에 git branch -r
. 내가 가진 것은 원격 주소뿐입니다 https://github.com/project-name/project-name.git
. 원격 주소로 원격 분기를 나열하는 방법이 있습니까? 유용한 정보를 찾을 수 없습니다.
$ git ls-remote --heads git@github.com:user/repo.git branch-name
branch-name
발견되는 경우 다음과 같은 출력이 표시됩니다.
b523c9000c4df1afbd8371324083fef218669108 refs/heads/branch-name
그렇지 않으면 출력이 전송되지 않습니다.
따라서 파이프를 연결 wc
하면 1
또는 0
다음 이 제공됩니다 .
$ git ls-remote --heads git@github.com:user/repo.git branch-name | wc -l
또는 일치하는 참조가없는 경우 종료 코드 를 반환하는 --exit-code
플래그를 설정할 수 있습니다 .git ls-remote
2
git ls-remote --heads https://github.com/rails/rails.git
5b3f7563ae1b4a7160fda7fe34240d40c5777dcd refs/heads/1-2-stable
81d828a14c82b882e31612431a56f830bdc1076f refs/heads/2-0-stable
b5d759fd2848146f7ee7a4c1b1a4be39e2f1a2bc refs/heads/2-1-stable
c6cb5a5ab00ac9e857e5b2757d2bce6a5ad14b32 refs/heads/2-2-stable
e0774e47302a907319ed974ccf59b8b54d32bbde refs/heads/2-3-stable
13ad87971cc16ebc5c286b484821e2cb0fc3e3b1 refs/heads/3-0-stable
3df6c73f9edb3a99f0d51d827ef13a439f31743a refs/heads/3-1-stable
f4db3d72ea564c77d5a689b850751ce510500585 refs/heads/compressor
c5a809e29e9213102351def7e791c3a8a67d7371 refs/heads/deps_refactor
821e15e5f2d9ef2aa43918a16cbd00f40c221e95 refs/heads/encoding
8f57bf207ff4f28fa8da4544ebc573007b65439d refs/heads/master
c796d695909c8632b4074b7af69a1ef46c68289a refs/heads/sass-cleanup
afd7140b66e7cb32e1be58d9e44489e6bcbde0dc refs/heads/serializers
실행할 git repo 인 경우 현재 폴더에서 사용할 수있는 또 다른 방법
git branch -a | egrep 'remotes/origin/${YOUR_BRANCH_NAME}$'
다음을 사용할 수도 있습니다.
git show-branch remotes/origin/<<remote-branch-name>>
$?의 최신 커밋과 값을 반환합니다. 값이 0이면 "심각 : 불량 sha1 참조 원격 / 원점 / <>"및 $? 128입니다
Bash 터미널에서 이와 같이 할 수 있습니다. 에코를 실행하려는 명령으로 바꾸십시오.
if git ls-remote https://username:password@github.com/project-name/project-name.git | grep -sw "remote_branch_name" 2>&1>/dev/null; then echo "IT EXISTS..START MERGE" ; else echo "NOT FOUND" ; fi
도움이되기를 바랍니다.
$ git ls-remote --heads origin <branch> | wc -l
대부분의 경우 작동합니다.
그러나 분기가 아래와 같이 부분적으로 일치하면 작동하지 않습니다.
$ git branch -a
creative/dev
qa/dev
$ git ls-remote --heads origin dev | wc -l
2
사용하다
git ls-remote --heads origin <branch> | \
cut -d$'\t' -f2 | \
sed 's,refs/heads/,,' | \
grep ^<branch>$ | wc -l
신뢰할 수있는 방법을 원한다면.
스크립트에서 사용 origin
하고 기본 원격 으로 가정하지 않으려면
git ls-remote --heads $(git remote | head -1) "$branch" | \
cut -d$'\t' -f2 | \
sed 's,refs/heads/,,' | \
grep ^"$branch"$ | wc -l
작동해야합니다.
참고 git branch -a | grep ...
마지막 이후 잠시 될 수 있으므로 신뢰할 수 없습니다 fetch
실행되었습니다.
그러면 매번 수동으로 저장소 이름을 전달할 필요가 없습니다.
git ls-remote origin <branch>
대신에
git ls-remote <full repo url> <branch>
예 :
git ls-remote git@bitbucket.org:landmarkgroupme/in-store-application.git uat_21dec
또는
git ls-remote origin uat_21dec
둘 다 동일한 출력을 제공합니다.
More on Origin : Git has the concept of "remotes", which are simply URLs to other copies of your repository. When you clone another repository, Git automatically creates a remote named "origin" and points to it. You can see more information about the remote by typing git remote show origin .
All of the answers here are Linux shell-specific, which doesn't help very much if you're in an environment that doesn't support those sort of operations - for example, Windows' command prompt.
Fortunately git ls-remote
accepts an --exit-code
argument that returns 0 or 2 depending on whether the branch exists or not, respectively. So:
git ls-remote --exit-code --heads origin <branch-that-exists-in-origin>
will return 0, and
git ls-remote --exit-code --heads origin <branch-that-only-exists-locally>
will return 2.
For PowerShell, you can simply use the built-in shell handling semantics:
if (git ls-remote --heads origin <branch-that-exists-in-origin>) { $true } else { $false }
yields $true
, while:
if (git ls-remote --heads origin <branch-that-only-exists-locally>) { $true } else { $false }
yields $false
.
You can add the repository you have as a remote using git remote add something https://github.com/project-name/project-name.git
and then do a git remote show something
to get all information about the remote. This requires a network connection and is useful for human use.
Alternatively, do a git fetch something
. This will fetch all branches on the remote called something
and keep them in your local repository. You can then merge them into your local branch as you please. I recommend this path since if you finally decide that you have to merge, this is what you need to do.
OT: Your use of "checked out locally" indicates that you're approaching this from a centralised version control system standpoint. That's usually a dead end when you're dealing with git. It uses words like "checkout" etc. differently from how older systems did.
You can try
git diff --quiet @{u} @{0}
Here @{u}
refers to remote/upstream, and @{0}
refers to current local HEAD (with newer version of git, @{0}
can be shortened as @
). If remote does not exist, it errors out.
With git 2.16.2 (I am not sure which version is the first to have this functionality, for example, git 1.7.1 doesn't have it), you can do
git checkout
If a remote branch exists, there will be some output, like
Your branch is up to date with 'origin/master'
Otherwise there is no output.
If your branch names are very specific, you might not need to use grep for simple branch name matching:
git ls-remote --heads $(git remote | head -1) "*${BRANCH_NAME}*" | \
cut -d$'\t' -f2 | \
sed 's,refs/heads/,,' | \
wc -l
which works for
BRANCH=master
BRANCH=mas
BRANCH=NonExistingBranch (returns 0)
BRANCH=ISSUE-123
We use unique issue id as branch names and it works fine.
I just tried this:
git ls-remote --heads 2>/dev/null|awk -F 'refs/heads/' '{print $2}'|grep -x "your-branch"|wc -l
This will return 1 if branch "your-branch" is found and 0 otherwise.
I'm combining some of the answers above in a script:
BRANCHES=(develop master 7.0 7.0-master)
ORIGIN=bitbucket
REMOTE=github
for BRANCH in "${BRANCHES[@]}"; do
BRANCH=$(git ls-remote --heads "${ORIGIN}" "${BRANCH}" \
| cut --delimiter=$'\t' --fields=2 \
| sed 's,refs/heads/,,' \
| grep --line-regexp "${BRANCH}")
if [ -n "${BRANCH}" ]
then
git branch --force "${BRANCH}" "${ORIGIN}"/"${BRANCH}"
git checkout "${BRANCH}"
git push "${REMOTE}" "${BRANCH}"
fi
done
git push github --tags
This script will get 4 branches from a remote bitbucket, and push them to a remote github, and will then push all tags to github. I'm using this in a Jenkins job, that's why you don't see any git fetch
or git pull
, that is already done in the Jenkins job repository config.
I usually prefer long-form options in scripts. I could have combined git branch
and git checkout
by using git checkout -B
.
Will return all branches (remote or local) that contain the query in the name.
git branch --all | grep <query>
'Programing' 카테고리의 다른 글
고정 된 크기의 객체 배열을 만드는 방법 (0) | 2020.09.15 |
---|---|
URL을 사용하여 마커가있는 Google지도에 연결 (0) | 2020.09.15 |
특정 디렉토리의 디렉토리 수 계산 (0) | 2020.09.15 |
여러 글꼴 두께, 하나의 @ font-face 쿼리 (0) | 2020.09.15 |
Angular2-Http POST 요청 매개 변수 (0) | 2020.09.15 |