How to create a remote branch with GIT

There are two ways to create a remote branch with git.

please replace ${name} with your real branch name.

Create local branch, then add to remote server

  • Create a new local branch ${name} with the command below:
git branch ${name}
  • Switch to your new branch
git checkout ${name}
  • Check Which branch you are using now:
git branch -a
  • Now create the remote branch:
git push --all
  • Edit git config
vi ./.git/config;
  • add the code in config
[branch "${name}"]
          remote = origin
          merge = refs/heads/${name}
  • then save the config file. OR you can use the command to track the remote branch
git branch --set-upstream ${name} origin/${name}

Create remote branch first

  • Create the remote branch
git push origin origin:refs/heads/${name}
  • Update origin
git fetch origin
  • show remote branch
git branch -r
  • Tracking the new branch
git checkout -tb ${name} origin/${name}
  • now you are using ${name} branch
  • update your branch
git pull

automate shell script

There are some automate shell script for create it.

Geoff Lane’s shell script

# git-create-branch branch_name
if [ $# -ne 1 ]; then
         echo 1>&2 Usage: $0 branch_name
         exit 127
git push origin master:refs/heads/$branch_name
echo "git push origin master:refs/heads/$branch_name"
git fetch origin
git checkout --track -b $branch_name origin/$branch_name
git pull

add this to .git/config.

publish = !sh -c ‘git push origin \”$0\” && git config \”branch. \
$0.remote\” origin && git config \”branch.$0.merge\” \”refs/heads/$0\”

then use command

git publish ${name}

windows CMD script

@set argC=0
@for %%x in (%*) do Set /A argC+=1
IF %argC% == 1 (
	set branch_name=%1
	git push origin origin:refs/heads/%branch_name%
	git fetch origin
	git checkout --track -b %branch_name% origin/%branch_name%
	git pull
) ELSE (
	echo Usage: %0 branch_name