Categories
branch git git-branch

How do I create a remote Git branch?

3564

I created a local branch. How do I push it to the remote server?

UPDATE: I have written a simpler answer for Git 2.0 here.

5

391

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

6

  • 6

    I find git push -u origin HEAD as answered here a bit more verbose (you write what you are doing) without being too much to type. Furthermore, a git push -u without additional arguments did not work for me if the branch was created with -t

    – Qw3ry

    Sep 25, 2017 at 7:37


  • git config --global push.default upstream && git checkout -b foo && <change a file> && git push -u does not work (as of git 2.19.1); push requires the remote and branch arguments.

    – knite

    Nov 7, 2018 at 2:37


  • Could you expand on what you mean by git co remote_branch_name?

    Sep 16, 2019 at 20:35

  • 1

    Didn’t work for me. Solved by adding to ~/.profile: function gitb() { git checkout -b $1 && git push --set-upstream origin $1; } and then can do gitb feature/abcd – this sets it up for git pull and git push without extra arguments + pushes the new branch into the remote repo to verify that the name is really free.

    – youurayy

    Jun 9, 2020 at 20:35

  • @youurayy you probably forgot also to add -u flag to git push -u and also check your git config --list that it contains push.default=current For me, git push -u just works. Also be aware that as of Git v2.27, you are prompted to set a default push strategy. So maybe your defaults are not different?

    Jun 10, 2020 at 6:28

981

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

2

  • 2

    Is there a way to create a remote branch without creating a local branch of the same name?

    May 1, 2020 at 18:35

  • @ArielGabizon Not 100% sure what you mean since you usually start your work locally (i.e. local branch named first) or pull an existing remote branch. But you can specify a new name when you initially push it to remote: git push -u origin [local-branch-name]:[remote-branch-name]. You can also specify your own branch name when you pull a remote branch: git branch -t -b [local-branch-name] origin/[remote-branch-name].

    Aug 29, 2021 at 21:43

391

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

6

  • 6

    I find git push -u origin HEAD as answered here a bit more verbose (you write what you are doing) without being too much to type. Furthermore, a git push -u without additional arguments did not work for me if the branch was created with -t

    – Qw3ry

    Sep 25, 2017 at 7:37


  • git config --global push.default upstream && git checkout -b foo && <change a file> && git push -u does not work (as of git 2.19.1); push requires the remote and branch arguments.

    – knite

    Nov 7, 2018 at 2:37


  • Could you expand on what you mean by git co remote_branch_name?

    Sep 16, 2019 at 20:35

  • 1

    Didn’t work for me. Solved by adding to ~/.profile: function gitb() { git checkout -b $1 && git push --set-upstream origin $1; } and then can do gitb feature/abcd – this sets it up for git pull and git push without extra arguments + pushes the new branch into the remote repo to verify that the name is really free.

    – youurayy

    Jun 9, 2020 at 20:35

  • @youurayy you probably forgot also to add -u flag to git push -u and also check your git config --list that it contains push.default=current For me, git push -u just works. Also be aware that as of Git v2.27, you are prompted to set a default push strategy. So maybe your defaults are not different?

    Jun 10, 2020 at 6:28