Reputation: 3516
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?
Upvotes: 5
Views: 3795
Reputation: 2896
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.
Upvotes: 1
Reputation: 212584
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
Upvotes: 13