How do you create a remote Git branch?

ID : 133

viewed : 138

Tags : gitbranchgit-branchgit





Top 5 Answer for How do you create a remote Git branch?

vote vote

99

First, you create your branch locally:

git checkout -b <branch-name> # Create a new branch and check it out 

The remote branch is automatically created when you push it to the remote server. So when you feel ready for it, you can do:

git push <remote-name> <branch-name>  

Where <remote-name> is typically origin, the name which git gives to the remote you cloned from. Your colleagues would then just pull that branch, and it's automatically created locally.

Note however that formally, the format is:

git push <remote-name> <local-branch-name>:<remote-branch-name> 

But when you omit one, it assumes both branch names are the same. Having said this, as a word of caution, do not make the critical mistake of specifying only :<remote-branch-name> (with the colon), or the remote branch will be deleted!

So that a subsequent git pull will know what to do, you might instead want to use:

git push --set-upstream <remote-name> <local-branch-name>  

As described below, the --set-upstream option sets up an upstream branch:

For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands.

vote vote

89

First, you must create your branch locally

git checkout -b your_branch 

After that, you can work locally in your branch, when you are ready to share the branch, push it. The next command push the branch to the remote repository origin and tracks it

git push -u origin your_branch 

Teammates can reach your branch, by doing:

git fetch git checkout origin/your_branch 

You can continue working in the branch and pushing whenever you want without passing arguments to git push (argumentless git push will push the master to remote master, your_branch local to remote your_branch, etc...)

git push 

Teammates can push to your branch by doing commits and then push explicitly

... work ... git commit ... work ... git commit git push origin HEAD:refs/heads/your_branch 

Or tracking the branch to avoid the arguments to git push

git checkout --track -b your_branch origin/your_branch ... work ... git commit ... work ... git commit git push 
vote vote

79

Simple Git 2.0+ solution:

As of Git 2.0, the behavior has become simpler:

You can configure git with push.default = current to make life easier:

I added this so now I can just push a new branch upstream with

$ git push -u 

-u will track remote branch of the same name. Now with this configuration, you will auto-guess the remote reference to git push. From git.config documentation:

push.default

Defines the action git push should take if no refspec is explicitly given.

push.default = current - push the current branch to update a branch with the same name on the receiving end. Works in both central and non-central workflows.

For me, this is a good simplification of my day-to-day Git workflow. The configuration setting takes care of the 'usual' use case where you add a branch locally and want to create it remotely. Also, I can just as easily create local branches from remotes by just doing git co remote_branch_name (as opposed to using --set-upstream-to flag).

I know this question and the accepted answers are rather old, but the behavior has changed so that now configuration options exist to make your workflow simpler.

To add to your global Git configuration, run this on the command line:

$ git config --global push.default current 
vote vote

60

As stated in the previous answers,

git push <remote-name> <local-branch-name>:<remote-branch-name> 

is enough for pushing a local branch.

Your colleagues, can pull all remote branches (including new ones) with this command:

git remote update 

Then, to make changes on the branch, the usual flow:

git checkout -b <local-branch-name> <remote-name>/<remote-branch-name> 
vote vote

54

Create a new branch locally based on the current branch:

git checkout -b newbranch 

Commit any changes as you normally would. Then, push it upstream:

git push -u origin HEAD 

This is a shortcut to push the current branch to a branch of the same name on origin and track it so that you don't need to specify origin HEAD in the future.

Top 3 video Explaining How do you create a remote Git branch?







Related QUESTION?