How to check if remote branch exists on a given remote repository?

后端 未结 14 944
独厮守ぢ
独厮守ぢ 2020-12-02 11:04

I need to do a subtree merge for a specific branch, if it exists on a given remote repository. The problem is that the remote repository is not checked out locally, so I can

相关标签:
14条回答
  • 2020-12-02 11:41

    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.

    0 讨论(0)
  • 2020-12-02 11:41

    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.

    0 讨论(0)
提交回复
热议问题