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, and place it at the root of your git project. Run it in the terminal or console as

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
  echo "Cancelled!";
When you purchase through links on, I may earn an affiliate commission.