#!/bin/bash branches=`git for-each-ref --format='%(refname:short)' refs/heads`; branchexists=false; for branch in ${branches}; do if [ "$branch" = "$1" ]; then branchexists=true; break; fi done if [ "$branchexists" == false ]; then echo "Branch $1 does not exist" exit 1 fi read -n1 -p "Switching to branch $1. Save changes on this branch? (Y/N/Q): " answer; if [ "$answer" == "Y" ] || [ "$answer" == "y" ]; then echo "Saving changes..."; git add .; git commit -m "Saving changes before switching to branch $1"; git push -u origin $1; elif [ "$answer" == "N" ] || [ "$answer" == "n" ]; then echo "Discarding changes..."; git reset --hard; elif [ "$answer" == "Q" ] || [ "$answer" == "q" ]; then printf "\nQuitting..."; exit 0; else echo "Invalid input. Quitting..."; exit 1; fi echo "Switching to branch $1..."; checkoutresult=`git checkout $1`; echo $checkoutresult if [ "$checkoutresult" == *"Switched to branch '$1'"* ]; then echo "Switched to branch $1"; else echo "Failed to switch to branch $1"; exit 1; fi echo "Installing node_modules for branch..."; npm install; echo "Finished switching to branch $1"; exit 0;