Shell Script to Delete and Recreate a Branch From Master

Both locally and remotely

To delete a branch and recreate it, both locally and remotely, here is a short shell script that asks for user confirmation and then proceeds to recreate the branch from master. In this example, the branch is development, the confirmation message is colored red to emphasize precuation, and the script only proceeds once user types exact yes.

Let’s put this script in recreate-development.sh, and place it at the root of your git project. Run it in the terminal or console as recreate-development.sh.

read -p $'\e[31mEnter "yes" to delete development branch & recreate it from master:\e[0m ' CONT
if [ "$CONT" = "yes" ]; then
  git checkout master
  git branch -d development #delete locally
  git push origin --delete development #delete remotely
  git pull origin master
  git checkout -b development
  git push origin development
else
  echo "Cancelled!";
fi
git