How do I push a new local branch to a remote Git repository and track it too?

后端 未结 15 1332
逝去的感伤
逝去的感伤 2020-11-22 09:17

I want to be able to do the following:

  1. Create a local branch based on some other (remote or local) branch (via git branch or git checkout

15条回答
  •  逝去的感伤
    2020-11-22 10:03

    Building slightly upon the answers here, I've wrapped this process up as a simple Bash script, which could of course be used as a Git alias as well.

    The important addition to me is that this prompts me to run unit tests before committing and passes in the current branch name by default.

    $ git_push_new_branch.sh
    
      Have you run your unit tests yet? If so, pass OK or a branch name, and try again
    
      usage: git_push_new_branch {OK|BRANCH_NAME}
    
      e.g.
    
      git_push_new_branch           -> Displays prompt reminding you to run unit tests
      git_push_new_branch OK        -> Pushes the current branch as a new branch to the origin
      git_push_new_branch MYBRANCH  -> Pushes branch MYBRANCH as a new branch to the origin
    

    git_push_new_branch.sh

    function show_help()
    {
      IT=$(cat < Displays prompt reminding you to run unit tests
      git_push_new_branch.sh OK        -> Pushes the current branch as a new branch to the origin
      git_push_new_branch.sh MYBRANCH  -> Pushes branch MYBRANCH as a new branch to the origin
    
      )
      echo "$IT"
      exit
    }
    
    if [ -z "$1" ]
    then
      show_help
    fi
    
    CURR_BRANCH=$(git rev-parse --abbrev-ref HEAD)
    if [ "$1" == "OK" ]
    then
      BRANCH=$CURR_BRANCH
    else
      BRANCH=${1:-$CURR_BRANCH}
    fi
    
    git push -u origin $BRANCH
    

提交回复
热议问题