#!/bin/sh Usage() { cat <<__USAGE__ Usage: git-empty-branch -h|--help git-empty-branch [-m|--message ""] branch ... __USAGE__ } MESSAGE="Empty branch" if [ $# -eq 0 ]; then Usage >&2 exit 1 fi BRANCHES= NOCOMMIT= while [ $# -ne 0 ]; do case $1 in --) shift BRANCHES="$BRANCHES $@" ;; -h|--help) Usage exit 0 ;; -m|--message) MESSAGE=$2 shift 2 ;; -n|--no-commit) NOCOMMIT=1 shift ;; *) BRANCHES="$BRANCHES $1" shift ;; esac done for B in $BRANCHES; do if [ -d .git/refs/heads/$B ]; then echo "branch $B already exists!" >&2 else git stash git symbolic-ref HEAD refs/heads/$B rm .git/index git clean -f -d [ -z "$NOCOMMIT" ] || exit 0 git commit --allow-empty -m "$MESSAGE" fi done