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

后端 未结 14 1022
独厮守ぢ
独厮守ぢ 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.

提交回复
热议问题