title: Bash script on MacOS to delete current git branch and prune origin
body: <p>Trying to write a bash script to put in my bash profile that deletes the current branch and prunes origin (intended for use after I've merged a remote pull request), with the following caveats:</p>
  
  <ol>
  <li>Cannot delete master</li>
  <li>requires a <code>-f</code> flag if the working branch is not clean OR where a -D flag would normally be required by git to successfully delete a branch.</li>
  </ol>
  
  <p>The problem I'm hitting is that the <code>-f</code> flag isn't being picked up.</p>
  
  <pre><code>function deleteCurrentBranch() {
    branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
  
    echo -e "Current branch is '$branch'"
    if [ $branch == "master" ]; then
      echo "This script cannot be used to delete the master branch!"
      return 1
    fi
  
    while getopts ":f" opt; do
       case ${opt} in
         f) 
            echo -e "Using the force!\n"
            force=true
            ;;
        *)
        echo "Invalid option: -$OPTARG" &gt;&amp;2
        ;;
      esac
    done
  
    echo "Using the force? $force"
  
    if [ $force == true ]; then
      echo -e "git reset --hard\n"
      git reset --hard
    else
      if [ -z "$(git status --porcelain)" ]; then
        force=false
      else
        echo -e "Working directory not clean and not using the force, exiting"
        return 1
      fi
    fi
  
    echo -e "git checkout master\n"
    git checkout master
  
    echo -e "\ngit pull"
    git pull
  
    deleteCommand="git branch -"
    if [ $force == true ]; then
      deletecommand+="D"
    else
      deleteCommand+="d"
    fi
  
    deleteCommand+=" $branch"
    echo -e $deleteCommand
    eval "$deleteCommand"
  
    echo -e "\ngit remote prune origin"
    git remote prune origin
  
    return 0
  }
  </code></pre>
  
  <p>Edit: I've spent a good amount of time on this tutorial page trying to figure this out
  <a href="https://wiki.bash-hackers.org/howto/getopts_tutorial" rel="nofollow noreferrer">https://wiki.bash-hackers.org/howto/getopts_tutorial</a></p>