git switch all projects on your local from one branch to other

Many a times we have to switch projects from one git branch to other in our workspaces. With handful of projects it is easy to do in your editor or to use command line.

However if the number if projects is bigger, it become monotnous boring and cumbersome.

Following is the simple shell script to help with that. The prerequisite is that all projects must be in same directory and your credentials are already set and saved for git repository.

The script will show all branches for suggestion. Just select the one you want to switch to by simple copy and paste.




#!/bin/bash
COL='\033[0;33m' #Yellow
RED_COL='\033[0;31m'
RESETCOL='\033[0m'
unset SELECTED_BRANCH
START_DIR=`pwd`
echo "select a branch you want to switch, from listed branches"
for i in */ ; do
  if [ -d "$i/.git" ]; then
    echo -e "${COL}--##__ $i __##--${RESETCOL}"
    cd $i
    if [ -z $SELECTED_BRANCH ]; then
         branches=`git branch -a| sed -e 's/remotes\/origin\///g'`
         echo "$branches"
         echo "which branch to switch to ?"
         echo -e "${COL}"
         read -p "Which branch to switch (copy from one listed above) : "  SELECTED_BRANCH
         echo -e "${RESETCOL}"
         if [[ $branches != *"$SELECTED_BRANCH"* ]];then
            echo -e "${RED_COL}No such branch exists. Exiting program.${RESETCOL}"
            exit 0
         fi
    fi
    git checkout $SELECTED_BRANCH
    git pull
    echo -e ""
    cd $START_DIR
  fi
done

Comments

Popular posts from this blog

Caused by: java.sql.SQLTimeoutException: ORA-01013: user requested cancel of current operation

HashiCorp Vault Integration with Ansible Etower using approle

utility to extract date from text with java