octavian-nita
12/14/2018 - 10:34 AM

Git :: rename branch

#!/usr/bin/env bash

# Consider writing robust bash shell scripts ( http://mywiki.wooledge.org/ ;)

set -o nounset

# ----------------------
# Command line arguments
# ----------------------

declare B_OLD_NAME=
declare B_NEW_NAME=

case $# in
1)
  B_OLD_NAME=`git rev-parse --abbrev-ref HEAD`
  B_NEW_NAME="$1"
  ;;
2)
  B_OLD_NAME="$1"
  B_NEW_NAME="$2"
  ;;
*)
  printf "\nusage:\n\t`basename ${BASH_SOURCE}` [<old-name>] <new-name>\n" 1>&2
  return 1
esac

# ----
# Main
# ----

printf "\nRenaming branch ${B_OLD_NAME} to ${B_NEW_NAME}...\n"

git branch -m ${B_OLD_NAME} ${B_NEW_NAME}         \
  && git push origin :${B_OLD_NAME} ${B_NEW_NAME} \
  && git push origin -u ${B_NEW_NAME}

printf "\nDone.\n"