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
First use git remote update , to bring your remote refs up to date. Then you can do one of several things, such as: git status -uno will tell you whether the branch you are tracking is ahead, behind or has diverged. If it says nothing, the local and remote are the same.
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';
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