57

I create a new branch like this:

git branch dev-itt-9

However, it only creates a new branch on local

git branch -a

* dev-itt-9
  master
  testing

  remotes/origin/HEAD -> origin/master
  remotes/origin/development
  remotes/origin/master
  remotes/origin/testing

What is the proper way to create a new branch on both local and remote?

I am quite new to git. Sorry if my question is stupid.

chipbk10
  • 5,783
  • 12
  • 49
  • 85

2 Answers2

81

First, you create your branch locally:

git checkout -b <branch-name>

The remote branch is automatically created when you push it to the remote server. So when you feel ready for it, you can just 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.

Credit: this answer is a copy of https://stackoverflow.com/a/1519032

MrFregg
  • 216
  • 1
  • 2
  • 13
Deepak Biswal
  • 4,280
  • 2
  • 20
  • 37
  • can I use "remotes/origin" instead of just pure "origin"? What is the difference? – chipbk10 Nov 05 '15 at 13:52
  • 1
    remote name is origin. So basically as per your question you have to do : `git push origin dev-itt-9`. So it will create origin/dev-itt-9 on your remote. – Deepak Biswal Nov 05 '15 at 13:53
  • 1
    @DeepakBiswal It appears you've copied this answer from https://stackoverflow.com/a/1519032 You're using someone else's work without giving the author credit. This amounts to plagiarism, and is not welcome on Stack Overflow. Remember to always add prominent attribution when using other sources. Thanks! – MrFregg Jun 02 '20 at 09:59
36

Suppose you already created your local branch (using git branch <branch-name> or git checkout -b <branch-name>), and that you are on the brancfh that you want to push (using git checkout <branch-name> for example) you can use:

git push -u origin <branch-name>

explications:

  • -u = --set-upstream : set this new remote branch as tracking branch.
  • origin : the name of your remote repository
Chris Maes
  • 35,025
  • 12
  • 111
  • 136