Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if branch exists in git repo without making a clone

Tags:

git

bash

jenkins

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

I make a clone of the single 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

It workes right, but it still takes a lot of time to clone a branch. Is there a way to make it work faster (maybe without making clone of repo or with interrupting of cloning if the branch is found)?

Thanks in advance

like image 507
dice2011 Avatar asked Oct 30 '22 09:10

dice2011


1 Answers

You could parse the output of

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

From its documentation:

Displays references available in a remote repository along with the associated commit IDs.

like image 178
Jens Hoffmann Avatar answered Nov 13 '22 04:11

Jens Hoffmann