How to create a new branch on both local and remote? GIT
Solution 1
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
Solution 2
Suppose you already created your local branch (using git branch <branch-name>
or git checkout -b <branch-name>
, 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
chipbk10
Updated on July 09, 2022Comments
-
chipbk10 almost 2 years
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 over 8 years<branch-name> in my case is dev-itt-9 right? So why not "git push -u remotes/origin dev-itt-9" ?
-
Chris Maes over 8 yearsnot 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 -
chipbk10 over 8 yearscan I use "remotes/origin" instead of just pure "origin"? What is the difference?
-
Deepak Biswal over 8 yearsremote 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. -
MrFregg almost 4 years@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!