I'm trying to write a shell script that should fail out if the current git branch doesn't match a pre-defined value.
if $gitBranch != 'my-branch'; then
echo 'fail'
exit 1
fi
Unfortunately, my shell scripting skills are not up to scratch: How do I get the current git branch in my variable?
To get the name of the current branch: git rev-parse --abbrev-ref HEAD
So, to check:
if test "$(git rev-parse --abbrev-ref HEAD)" != my-branch; then
echo Current branch is not my-branch >&2
exit 1
fi
You can get the branch using git branch
and a regex:
$gitBranch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
After that you just have to make your test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With