xXxrk04
xXxrk04

Reputation: 195

SHELL: GIT branch to variable

I need some help. How can i get the value of git branch to be in a variable to check for if and else condition.

 COMMIT_MESSAGE="Update"
 BRANCH_PUSH="master"
 git branch
 if [git branch == $BRANCH_PUSH]; then
    echo "Success"
 else   
     echo "Error"
 fi                  

Upvotes: 0

Views: 134

Answers (2)

the1dv
the1dv

Reputation: 931

To get the git branch you can do it a few ways but this is one way:

git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3

Lovely one liner :)

so:

COMMIT_MESSAGE="Update"
BRANCH_PUSH="master"
git-branch = "$(git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3)"
if [$git-branch == "$BRANCH_PUSH"]; then
    echo "Success"
else   
    echo "Error"
fi                  

Upvotes: 0

peter
peter

Reputation: 94

This could do it:

git_branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
if [[ "$git_branch" == "$BRANCH_PUSH" ]]; then
  echo "success"
else
  echo "error"
fi

Upvotes: 1

Related Questions