Issue
I am very bad at shell scripting (with bash), I am looking for a way to check if the current git branch is "x", and abort the script if it is not "x".
#!/usr/bin/env bash
CURRENT_BRANCH="$(git branch)"
if [[ "$CURRENT_BRANCH" -ne "master" ]]; then
echo "Aborting script because you are not on the master branch."
return; # I need to abort here!
fi
echo "foo"
but this is not quite right
Solution
Use git rev-parse --abbrev-ref HEAD
to get the name of the current branch.
Then it's only a matter of simply comparing values in your script:
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
if [[ "$BRANCH" != "x" ]]; then
echo 'Aborting script';
exit 1;
fi
echo 'Do stuff';
Answered By - knittl Answer Checked By - Katrina (WPSolving Volunteer)