I want to be able to do the following:
Create a local branch based on some other (remote or local) branch (via git branch
or git checkout
You can do it in 2 steeps:
1. Use the checkout
for create the local branch:
git checkout -b yourBranchName
Work with your Branch as you want.
2. Use the push
command to autocreate the branch and send the code to the remote repository:
git push -u origin yourBanchName
There are mutiple ways to do this but I think that this way is really simple.
To create a new branch by branching off from an existing branch
git checkout -b <new_branch>
and then push this new branch to repository using
git push -u origin <new_branch>
This creates and pushes all local commits to a newly created remote branch origin/<new_branch>
For greatest flexibility, you could use a custom Git command. For example, create the following Python script somewhere in your $PATH
under the name git-publish
and make it executable:
#!/usr/bin/env python3
import argparse
import subprocess
import sys
def publish(args):
return subprocess.run(['git', 'push', '--set-upstream', args.remote, args.branch]).returncode
def parse_args():
parser = argparse.ArgumentParser(description='Push and set upstream for a branch')
parser.add_argument('-r', '--remote', default='origin',
help="The remote name (default is 'origin')")
parser.add_argument('-b', '--branch', help='The branch name (default is whatever HEAD is pointing to)',
default='HEAD')
return parser.parse_args()
def main():
args = parse_args()
return publish(args)
if __name__ == '__main__':
sys.exit(main())
Then git publish -h
will show you usage information:
usage: git-publish [-h] [-r REMOTE] [-b BRANCH]
Push and set upstream for a branch
optional arguments:
-h, --help show this help message and exit
-r REMOTE, --remote REMOTE
The remote name (default is 'origin')
-b BRANCH, --branch BRANCH
The branch name (default is whatever HEAD is pointing to)
git pull origin master
git push
I simply do
git push -u origin localBranch:remoteBranchToBeCreated
over an already cloned project.
Git creates a new branch named remoteBranchToBeCreated
under my commits I did in localBranch
.
Edit: this changes your current local branch's (possibly named localBranch
) upstream to origin/remoteBranchToBeCreated
. To fix that, simply type:
git branch --set-upstream-to=origin/localBranch
or
git branch -u origin/localBranch
So your current local branch now tracks origin/localBranch
back.
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
function show_help()
{
IT=$(cat <<EOF
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.sh -> 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