59

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.

2
  • Do some changes to your branch and commit and push to origin! Commented Nov 5, 2015 at 13:44
  • git add ., git commit -m "your message", git push origin dev-itt-9 ! Commented Nov 5, 2015 at 13:44

2 Answers 2

83

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

Sign up to request clarification or add additional context in comments.

3 Comments

can I use "remotes/origin" instead of just pure "origin"? What is the difference?
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.
@DeepakBiswal It appears you've copied this answer from 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!
37

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

4 Comments

<branch-name> in my case is dev-itt-9 right? So why not "git push -u remotes/origin dev-itt-9" ?
not sure what side effect the "remotes/origin" would have. git push origin dev-itt-9 will do the trick for you; add -u if you want to track the (new) remote branch
I think you should remove method git branch <branch-name from this answer - when on other branch, one would have to checkout first anyway, am I right?
debatable. I made some modifications that might make my answer more clear

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.