Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script to check if the current git branch = "x"

Tags:

git

bash

shell

sh

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

like image 267
Alexander Mills Avatar asked Jun 17 '16 21:06

Alexander Mills


People also ask

How can I tell if a remote branch is advance?

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.


1 Answers

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'; 
like image 191
knittl Avatar answered Oct 10 '22 13:10

knittl