How do I create a remote Git branch?
Asked Answered
P

26

3709

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.

Parmesan answered 5/10, 2009 at 9:21 Comment(6)
did anyone ever answer you second question? >>And how would I tell my colleagues to pull it from the upstream repository?Irkutsk
Possibly related: Pro Git: 3.5 Git Branching - Remote Branches.Imaginary
@Irkutsk get fetch --all fetches the new branches on the remote side (but only a get fetch --prune deletes locally the references to the deleted remote branches). I think, either this should be set up by them automatically, or you have to talk to them verbally.Velar
Possible duplicate of How do I push a new local branch to a remote Git repository and track it too?Limber
There is a similar question here on Stack Overflow on how to track a newly created remote branch. - You haven't told us what question you have in mind. My guess would be stackoverflow.com/questions/520650. In line with the comment by @Trevor Boyd Smith I consider both your question here and that question duplicates of stackoverflow.com/questions/2765421.Athwart
works for me! March 2024Womenfolk
P
421

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
Parmesan answered 28/11, 2014 at 9:46 Comment(7)
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 -tSmokejumper
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.Maharanee
Could you expand on what you mean by git co remote_branch_name?Marchellemarcher
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.Microdont
@Microdont 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?Bedwarmer
@JesperRønn-Jensen see that is exactly the point -- one simple alias-like bash function, and you don't have to remember nor do any of those.Microdont
Depressingly, git has changed behavior and this no longer works.Maclay
I
4245

First, create a new local branch and check it out:

git checkout -b <branch-name>

The remote branch is automatically created when you push it to the remote server:

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

<remote-name> is typically origin, which is the name which git gives to the remote you cloned from. Your colleagues may then simply pull that branch.

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.

Inflationary answered 5/10, 2009 at 9:29 Comment(23)
Note that default behavior of git is to push matching refs, so git push <remote> would not push branch if it is not present on <remote>.Mountbatten
You might want to use git push -u <remote-name> <branch-name> instead, so that a subsequent git pull will know what to do.Sherlocke
Instead of explicitly specifying the server name, you can just use origin, which means "the server I got the rest of this repo from": thus git push origin <branch-name>.Dobruja
@Bart-Schuller, the -u is very important! I keep coming back to this question/answer to remember it.Carlyncarlynn
in my case git push <remote-name> <local-branch-name>:<remote-branch-name> works, but git push <remote-name> <branch-name> get error "error: src refspec current does not match any."Leandroleaning
If it's a new branch on the remote it seems it's not enough, I got: error: unable to push to unqualified destination: branch-name The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. I had to prefix the remote branch name with refs/ to make it work. Thus git push <remote-name> <local-branch-name>:<refs/remote-branch-name>.Mithridate
If you forget to use the -u option, you can just type git push -u afterwards in the branch, then git pull will work.Argot
@Argot If you don't specify a branch name, this will setup remote tracking on all of your branches. Not normally a bad thing, but something to be aware of.Olympe
Putting it all together, git push -u origin <local-branch-name> is what worked for me.Gilles
@AndresRiofrio That would create a new branch based on the current branch.Inflationary
@Snicolas What you described is possible, but not necessary. You can leave out the remote-branch-name and git will use the same name as the local branch.Inflationary
This solution doesn't works for me (maybe because I'm using the new default of push.default, which is simple). It works only if the local branch creation uses -t: git checkout -t -b your_branch. I'm wondering how to push a branch created without the -t, though.Schechinger
Arh! Where <remote-name> could be "origin"... Damn that took me a while to figure out [still noob]Fizzy
when to do git add and git commit ? before creating local branch or after that?Nutritionist
@Nutritionist Add and commit should happen after you created the branch, otherwise that commit would be created on the current branch.Inflationary
Is your_branch and branch-name the same? Is my remote name origin or master? I'm a little confused hereSerenata
@HrvojeT I made the branch-name more consistent. The remote name is explained in the post, but master is (almost) never the name of a remote (unless someone gets it into their mind to call their remote master).Inflationary
So, origin is a remote repository, and master and branch-name are branches in that remote repository, usuallySerenata
@jww If you read correctly, you see it says: git push <remote> <branch>. You failed to specify a remote, and just repeated the branch name twice.Inflationary
Is there any way that I can create a remote branch from another remote branch without fetching it locally?Danndanna
What -b does in git checkout -b <branch-name> ?Codicodices
@Codicodices It tells git checkout you want to create a new branch (instead of checking out an existing one or checking out a file).Inflationary
For those a little confused about the -u, here's a better explanation.Temblor
S
1019

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
Shimkus answered 26/7, 2011 at 1:9 Comment(5)
Is there a way to create a remote branch without creating a local branch of the same name?Coppola
@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].Kohlrabi
@Shimkus what's the utility of -u ?Withrow
@Withrow -u is a shortcut for --set-upstream , it updates your local branch registry to track the new created branch, it's , you need to use that option (either -u or --set-upstream, both are the same) if you want your future invokations of push where they have to send the information to, without having to be explicit about the involved branches everytimeShimkus
@ArielGabizon yes, it can be done like that, I do prefer to make explicit "refs/heads/[remote-branch-name]" after the ":" I like to have same name local branches to avoid confusions, but that use of the command push is very handy to manipulate remotes, including tagsShimkus
P
421

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
Parmesan answered 28/11, 2014 at 9:46 Comment(7)
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 -tSmokejumper
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.Maharanee
Could you expand on what you mean by git co remote_branch_name?Marchellemarcher
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.Microdont
@Microdont 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?Bedwarmer
@JesperRønn-Jensen see that is exactly the point -- one simple alias-like bash function, and you don't have to remember nor do any of those.Microdont
Depressingly, git has changed behavior and this no longer works.Maclay
L
95

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>
Lisandra answered 27/12, 2011 at 14:12 Comment(2)
I like this cause it allows the remote name to be different than the local oneCoppola
great, I can bar baz now.Tessi
S
94

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.

Shinbone answered 24/2, 2014 at 14:58 Comment(5)
This helped in my case: git push -u origin HEAD. I think it's the most clear way.Ultraconservative
Yeah, you never remember what you last typed as a branch, exactly, so this is the way to go.Cooking
@Cooking If you can't remember what branch you're on or what you named it, you probably shouldn't be pushing at all! At least, not without running git status first.Shinbone
Yeah, gotta make sure the world doesn't explode on that push; I agree.Cooking
This is the most efficient way to create a tracking branch as well as a remote branch at the same time. I'd also like to add git remote show origin as a third step just to visualize the new tracking/tracked relationship.Esme
T
84

If you want to create a branch from the current branch

git checkout -b {your_local_branch_name}

you want a branch from a remote branch, you can try

git checkout -b {your_local_branch_name} origin/<remote_branch_name>

If you are done with changes you can add the file.

git add -A or git add <each_file_names>

Then do a commit locally

git commit -m 'your commit message'

When you want to push to remote repo

git push -u origin <your_local_branch_name>

All together will be

git checkout -b bug_fixes 

or If you want to create a local branch bug_fixes from a remote branch, say development

git checkout -b bug_fixes origin/development

You can push to the branch to remote repo by

git push -u origin bug_fixes

Anytime you want to update your branch from any other branch say master,

git pull origin master
Tagliatelle answered 1/5, 2016 at 6:12 Comment(0)
F
70

[Quick Answer]

You can do it in 2 steps:

1. Use the checkout for create the local branch:

git checkout -b yourBranchName

2. Use the push command to autocreate the branch and send the code to the remote repository:

git push -u origin yourBranchName

There are multiple ways to do this but I think that this way is really simple.

Faradism answered 2/10, 2019 at 9:43 Comment(0)
P
52

If you wanna actually just create remote branch without having the local one, you can do it like this:

git push origin HEAD:refs/heads/foo

It pushes whatever is your HEAD to branch foo that did not exist on the remote.

Pulchritude answered 25/11, 2015 at 15:29 Comment(4)
Doing this completely confused my Visual Studio to the point where it wouldn't start up correctly. The Team Explorer wouldn't load at all, but everything else went bonkers throwing errors also. Just FYI.Nationality
This sounds like it should work, but when I actually tried it, the our gitlab server didn't recognise the result as a branch.Communize
What branch is foo branched off of on the remote? What if I wanted to foo to branch off of foo2? Is that possible? Thank you.Jonquil
It works on gerrit server as well, creating a new branch on the remote that didn't exist before this command.Socage
P
35

Easiest Solution... Drumm Roll... git version 2.10.1 (Apple Git-78)

1) git checkout -b localBranchNameThatDoesNotExistInRemote

2) Do your changes, and do a git commit 

3) git push origin localBranchNameThatDoesNotExistInRemote --force

N.B. - The branch you just created in your local environment, and the remote non-existing branch where you are trying to push, must have the same name.

Phosgene answered 18/3, 2017 at 6:23 Comment(1)
Thanks for your suggestion. Even though you call this an easy solution, I still think git push -u is way easier. Requires that you have one global config line, see https://mcmap.net/q/12516/-how-do-i-create-a-remote-git-branch. I use git push -u constantly, it covers 99% of my usecases when working.Bedwarmer
K
30

First you create the branch locally:

git checkout -b your_branch

And then to create the branch remotely:

git push --set-upstream origin your_branch

Note: This works on the latests versions of git:

$ git --version
git version 2.3.0

Cheers!

Kinescope answered 21/4, 2015 at 23:43 Comment(1)
This is just the help text generated by the command git push when your local branch is not tracked by a remote.Neurotic
O
22

you can simply,

  1. git checkout -b YOUR-NEW-BRANCH-NAME
  2. git add .
  3. git push origin YOUR-NEW-BRANCH-NAME

you can see your branch with the code under the relevant git repo

Cheers !! :)

Ormsby answered 13/4, 2021 at 18:10 Comment(0)
Y
20

Create the branch on your local machine and switch in this branch :

$ git checkout -b [name_of_your_new_branch]

Push the branch on github :

$ git push origin [name_of_your_new_branch]

When you want to commit something in your branch, be sure to be in your branch.

You can see all branches created by using :

$ git branch

Which will show :

* approval_messages
  master
  master_clean

Add a new remote for your branch :

$ git remote add [name_of_your_remote] 

Push changes from your commit into your branch :

$ git push origin [name_of_your_remote]

Update your branch when the original branch from official repository has been updated :

$ git fetch [name_of_your_remote]

Then you need to apply to merge changes, if your branch is derivated from develop you need to do :

$ git merge [name_of_your_remote]/develop

Delete a branch on your local filesystem :

$ git branch -d [name_of_your_new_branch]

To force the deletion of local branch on your filesystem :

$ git branch -D [name_of_your_new_branch]

Delete the branch on github :

$ git push origin :[name_of_your_new_branch]

Here All Information

Other Existing project

Yttriferous answered 27/5, 2016 at 11:39 Comment(0)
P
14

Creating a local branch from an existing branch (can be master/ develop/ any-other-branch).

git checkout -b branch_name

Push this to remote

git push -u remote_name local_branch_name:remote_branch_name

Here,

  1. -u : sets the upstream branch
  2. remote_name : git sets the name by default to be "origin" when it creates the repository. This can however be changed to a different arbitrary name.
  3. local_branch_name : is the name of the local branch to be pushed.
  4. remote_branch_name : is the name of the remote branch that we want to be created on remote.

If we remove the local and remote branch names, it will have the format

git push -u remote_name branch_name

This will push the local branch to remote and with the same name as the local branch branch_name. The local branch will be tracking the remote branch as well.

Piddling answered 15/2, 2017 at 6:57 Comment(0)
B
10

I know this question is well answered, but just wanted to list the steps I take to create a new branch "myNewBranch" and push to remote ("origin" in my case) and set up tracking. Consider this the "TL;DR" version :)

# create new branch and checkout that branch
git checkout -b myNewBranch
# now push branch to remote 
git push origin myNewBranch
# set up the new branch to track remote branch from origin
git branch --set-upstream-to=origin/myNewBranch myNewBranch
Bonni answered 23/10, 2014 at 14:9 Comment(0)
P
9

Just wanted to add that while:

git checkout -b {branchName}

Creates a new branch, it also checks out that branch / makes it your current branch. If, for some reason, all you want to do is snap off a branch but not make it your current branch, then you would use the following command:

git branch {branchName}

In the first command, "checkout" makes said branch your current branch, and the "-b" means: this branch doesn't exist yet, so make it for me.

Pest answered 30/3, 2015 at 14:2 Comment(0)
D
7

Now with git, you can just type, when you are in the correct branch

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

and git create for you the origin branch.

Deracinate answered 27/10, 2014 at 17:5 Comment(3)
-u is short for --set-upstream.. so command could be git push -u origin <remote-branch-name>Entelechy
fatal: 'origin' does not appear to be a git repositoryGt
maybe you have to set the origin of your git repositoryDeracinate
L
7

How to do through Source Tree

 1: Open SourceTree, click on Repository -> Checkout
 2: Click on Create New Branch
 3: Select the branch where you want to get code for new branch 
 4: Give your branch name
 5: Push the branch  (by click on Push-button)
Lemay answered 21/7, 2015 at 11:27 Comment(0)
D
4

git push -u <remote-name> <branch-name> doesn't work if the newly created branch isn't spawned from the same repo, i.e. if you haven't created the new branch using git checkout -b new_branch, then this will not work.

For eg, I had cloned two different repositories locally and I had to copy repo2/branch1 to repo1/ and then push it too.

This link helped me push my local branch (cloned from another repo) to my remote repo:

Downatheel answered 24/2, 2014 at 21:38 Comment(0)
B
4

Starting from Git 2.37.0, you do not need "--set-upstream origin" anymore. Just git push. You can achieve this with the push.autoSetupRemote option enabled

git config --global --add --bool push.autoSetupRemote true

Bartolommeo answered 6/10, 2022 at 15:56 Comment(0)
S
3

Here is how you do it in eclipse through Egit.

  1. Go the "Git Repository Exploring" view and expand the git project to which you want to create a branch. Under Branches -> Local .. select the branch for which you want to create the branch ( In my case I selected master .. you can select another branch if you wish) .. then right click and click on Create Branch option .. and select the checkout this project option and then click the finish button.

  2. Now from the project explorer select the project .. right click then Team -> Push Branch.

A new remote branch will be created. You can give the name of the branch to your colleagues so that they can pull it.

Stace answered 31/1, 2014 at 23:46 Comment(1)
Tangential warning about Egit -- and all JGit-based clients, AFAIK: they don't support .gitattributes! This means if your team uses a mix of Windows(CRLF) and Linux/OSX(LF) you must depend on each client having the right settings at all times. Naturally it's better to manage line endings centrally at the repo or project level, and .gitattributes is the supported way to do this. So, if you don't absolutely have to use Egit... don't! :)Modulate
P
1

I have used two ways to create branch

If you are using TortoiseGit follow these steps:-

1.Create Branch using TortoiseGit

Right click on your project >>> TortoiseGit >>> Create Branch >>> write the name of branch and select the base branch then press ok

2.Push the branch

Right click on your project >>> TortoiseGit >>> push >>> click ok

3.Switch to new branch

Right click on your project >>> TortoiseGit >>> Switch/Checkout >>> select newly created branch and press ok

If you are using command prompt follow these steps:-

1.Create branch using command prompt

$git checkout -b new_branch_name

2.Push the branch

$git push origin new_branch_name

3.Switch to new branch it will already switched to new_branch_name otherwise you can use

$git checkout new_branch_name

Paquette answered 7/1, 2020 at 6:1 Comment(0)
A
1

To create a new branch in remote, you can use the following command:

git checkout -b <new-branch-name>

For example, to create a new branch called "feature_branch", you would use the following command:

git checkout -b feature_branch

This will create a new branch locally, but it will not push it to the remote repository. To push the new branch to the remote repository, you can use the following command:

git push origin feature_branch

For example, to push the "feature_branch" branch to the remote repository, you would use the following command:

git push origin feature_branch

This will push the new branch to the remote repository, and it will be available to other users.

Here are some additional things to keep in mind about creating new branches in remote:

You can use the git branch -l command to list all of the branches in your local repository.

You can use the git checkout command to switch to a different branch.

You can use the git merge command to merge a branch into the current branch.

You can use the git reset --hard command to reset the current branch to the state of the specified branch.

Acerose answered 18/10, 2023 at 17:50 Comment(0)
D
-1

I use this and it is pretty handy:

git config --global alias.mkdir '!git checkout -b $1; git status; git push -u origin $1; exit;'

Usage: git mkdir NEW_BRANCH

You don't even need git status; maybe, I just want to make sure everything is going well...

You can have BOTH the LOCAL and REMOTE branch with a single command.

Dioxide answered 24/7, 2019 at 7:31 Comment(0)
M
-1

I've solved this by adding this into my bash ~/.profile:

function gitb() { git checkout -b $1 && git push --set-upstream origin $1; }

Then to start up a new local + remote branch, I write:

gitb feature/mynewbranch

This creates the branch and does the first push, not just to setup tracking (so that later git pull and git push work without extra arguments), but actually confirming that the target repo doesn't already have such branch in it.

Microdont answered 9/6, 2020 at 20:33 Comment(0)
J
-1

Heres an example I only have two branches that were local first: origin and mobile-test.

Nothing for me worked until I used this in command line to actually show my updated files in a remote branch.

git push --set-upstream origin mobile-test
Jungle answered 22/9, 2020 at 5:26 Comment(0)
U
-1

If you have used --single-branch to clone the current branch, use this to create a new branch from the current:

git checkout -b <new-branch-name>
git push -u origin <new-branch-name>
git remote set-branches origin --add <new-branch-name>
git fetch
Ulund answered 29/9, 2020 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.