Check if branch exists in git repository without creating clone

I have a huge git repository with a lot of branches. I need to check out a shell command inside Jenkins if there is a branch inside the repository.

I am making a clone of one branch like this

if `sshpass -p password git clone -b "${BRANCH}" --single-branch ssh://user@server/GIT/${REPO}.git`; then 
    echo "success"    
else
    echo "ERROR: There is no branch "${BRANCH}" inside repo "${REPO}""
    exit 42
fi

      

This works correctly, but it still takes a long time to clone the branch. Is there a way to make it run faster (perhaps without cloning the repo, or interrupting cloning if a branch is found)?

Thank you in advance

+3


source to share


2 answers


You can parse the output

git ls-remote http://user@server/GIT/${REPO}.git

      



From the documentation :

Displays the links available in the remote repository, along with associated commit IDs.

+2


source


To get only your branches, not tags or whatever you need to do,

git ls-remote -h https://github.com/danielgindi/Charts.git

      

PS: Replace your remote url.



This worked for me,

git ls-remote -h https://github.com/danielgindi/Charts.git

a7c7bb4caf7e68e1713468c3a021d0860a587ac8    refs/heads/independent-scatter
5b22fa99ef80f2004a7439ee6d3c1cfc9c4be010    refs/heads/legacy/v2
f23e872e7573f8c79862ff4822cfc5683703d424    refs/heads/master
03939328cd995043877f97daa1c75c0d5a3ab60c    refs/heads/xcode-8.3

      

Check it out on Github yourself.

0


source







All Articles