How do I push a new local branch to a remote Git repository and track it too?

4,704,365

Solution 1

In Git 1.7.0 and later, you can checkout a new branch:

git checkout -b <branch>

Edit files, add and commit. Then push with the -u (short for --set-upstream) option:

git push -u origin <branch>

Git will set up the tracking information during the push.

Solution 2

If you are not sharing your repo with others, this is useful to push all your branches to the remote, and --set-upstream tracking correctly for you:

git push --all -u

(Not exactly what the OP was asking for, but this one-liner is pretty popular)

If you are sharing your repo with others this isn't really good form as you will clog up the repo with all your dodgy experimental branches.

Solution 3

Prior to the introduction of git push -u, there was no git push option to obtain what you desire. You had to add new configuration statements.

If you create a new branch using:

$ git checkout -b branchB
$ git push origin branchB:branchB

You can use the git config command to avoid editing directly the .git/config file:

$ git config branch.branchB.remote origin
$ git config branch.branchB.merge refs/heads/branchB

Or you can edit manually the .git/config file to add tracking information to this branch:

[branch "branchB"]
    remote = origin
    merge = refs/heads/branchB

Solution 4

Simply put, to create a new local branch, do:

git branch <branch-name>

To push it to the remote repository, do:

git push -u origin <branch-name>

Solution 5

A slight variation of the solutions already given here:

  1. Create a local branch based on some other (remote or local) branch:

    git checkout -b branchname
    
  2. Push the local branch to the remote repository (publish), but make it trackable so git pull and git push will work immediately

    git push -u origin HEAD
    

    Using HEAD is a "handy way to push the current branch to the same name on the remote". Source: https://git-scm.com/docs/git-push In Git terms, HEAD (in uppercase) is a reference to the top of the current branch (tree).

    The -u option is just short for --set-upstream. This will add an upstream tracking reference for the current branch. you can verify this by looking in your .git/config file:

    Enter image description here

Share:
4,704,365
Roni Yaniv
Author by

Roni Yaniv

Updated on July 11, 2022

Comments

  • Roni Yaniv
    Roni Yaniv almost 2 years

    I want to be able to do the following:

    1. Create a local branch based on some other (remote or local) branch (via git branch or git checkout -b)

    2. Push the local branch to the remote repository (publish), but make it trackable so git pull and git push will work immediately.

    How do I do that?

    I know about --set-upstream in Git 1.7, but that is a post-creation action. I want to find a way to make a similar change when pushing the branch to the remote repository.

    • ed9w2in6
      ed9w2in6 over 10 years
      just to point out --set-upstream is -u
    • Admin
      Admin over 3 years
      lots of answers containing unrelated information (like how to create a branch) and if the answer applies, then information is missing regarding the magic parameters used.
    • Andreas
      Andreas over 3 years
      @VividD "added an illustrative picture" - Seriously? o.O
    • Kip
      Kip about 2 years
      my workflow is: git checkout -b branch, git push => it prints an error message containing the command you need to run. then copy/paste that command. :)
    • Yuri Aps
      Yuri Aps about 2 years
      @Kip or Anyone, is there a way to skip the copy/paste part?, I hate doind that, just intsalled the pip thefuck to avoid this but didn't work
  • Tobias Kienzler
    Tobias Kienzler about 14 years
    William's script I linked to does about the same with the additional option to delete remote branches and some safeguards, too
  • Geoff Gustafson
    Geoff Gustafson about 14 years
    >to push the local branch to remote repo (publish), but make it >trackable so git pull and git push will work immediately. its what github does automatically when you push your code to their repository :-)
  • Lohrun
    Lohrun about 14 years
    This does not respond to the question, the <newbranch> of the original repo is not trackable (and is renamed as <master> is the new repo you clone in step 3).
  • Roni Yaniv
    Roni Yaniv about 14 years
    this assumes I have ruby installed. no such luck. any other ideas?
  • Roni Yaniv
    Roni Yaniv about 14 years
    seems kind of overkill. does the git remote add origin make the local branch trackable? is that the key command here?
  • Lohrun
    Lohrun about 14 years
    @Roni Yaniv: no git remote add origin only register a new remote repository. It is just a step needed before pushing your branch to that remote repository (if you don't want to type the whole address each time)
  • Roni Yaniv
    Roni Yaniv about 14 years
    well - we already have a remote repo (origin). i need a way to add a new local branch to that repo while making it trackable+pushable+pullable in the process.
  • Lohrun
    Lohrun about 14 years
    the ruby script calls git push and git config command. I used the code of the script to edit my answer. You might used this information to create a small shell script that does the puslishing for you.
  • void.pointer
    void.pointer almost 10 years
    It's also worth noting that if you have an existing tracking branch already set on the branch you're pushing, and push.default is set to upstream, this will not do what you think it will do. It will try to push over the existing tracking branch. Use: git push -u origin mynewfeature:mynewfeature or do git branch --unset-upstream first.
  • Paul Whipp
    Paul Whipp almost 10 years
    I still needed to 'git branch --set-upstream-to origin/remote' in order for 'git status' to correctly report my branch status with respect to the remote branch.
  • commonpike
    commonpike over 9 years
    and git pull --all pulls it all back elsewhere ? kewl
  • Mike D
    Mike D over 9 years
    William's miscellaneous git tools appears to have moved (that link is now dead). A working link is: gitorious.org/willgit
  • amey91
    amey91 over 9 years
    This command sets up tracking to the correct branch without the need to push anything. Thank you.
  • Puterdo Borato
    Puterdo Borato about 9 years
    For people using Git from Visual Studio: Actually this is that "Publish Branch" in Visual Studio does. After executing git push with -u parameter i can finally see my branch as published in VS UI.
  • SidOfc
    SidOfc almost 9 years
    For those still confused about the concept I like to "THINK" of it that git push -u origin myepicbranchname is equal to git checkout -b myepicbranchname in terms of what it does. Git push does do more but in terms of simplicity this is most likely what you want to know about the two ;)
  • Arda
    Arda about 8 years
    In git push origin master:new_feature_name command, master is the branch that you're working on. If you're on a different branch (let's call it oldbranch, and want to push to a newbranch, the command will be like git push origin oldbranch:newbranch .
  • Federico Razzoli
    Federico Razzoli almost 8 years
    Git allows to commit a branch and not push it for very good reasons. Only using git push --all is like dropping a piece of git architecture. If it works for you, it is perfectly ok, great, do it forever. But PLEASE don't recommend others to avoid learning git just because it is a quick way to do things.
  • ScottJ
    ScottJ over 7 years
    "William's" link broken again; new link seems to be git-wt-commit.rubyforge.org
  • Robert
    Robert over 7 years
    git branch <branch-name> and git checkout -b <branch-name> both create a branch but checkout switch to the new branch
  • Daniel Tonon
    Daniel Tonon over 7 years
    Thank you :) git push -u origin <branch-name> wasn't working for me but using HEAD instead of <branch-name> worked perfectly :)
  • JonTheNiceGuy
    JonTheNiceGuy over 7 years
    Somewhat annoyingly, I found that git push -u origin branchname wasn't working, but that git push -u --set-upstream origin branchname did. This is with the PoshGit scripts provided with Github for Windows.
  • akronymn
    akronymn over 7 years
    This really isn't the right answer and isn't a good tool to be recommending without a real explanation of what it does and what the implications are. Please consider taking this answer down.
  • user1823664
    user1823664 almost 7 years
    @Federico @akronymn Where can one find the dangers of doing git push --all -u?
  • Jonathan Morales Vélez
    Jonathan Morales Vélez over 6 years
    Please bear in mind that if you want your remote branch to have a different name, you should change the name locally first. For example, let's say I have a branch locally named a-named-branch and I want to push it to remote. If I run git push origin new-named-branch I'll get an error that says error: src refspec new-named-branch does not match any. The fix is to run git branch -m new-named-branch and then running git push origin new-named-branch.
  • amaslenn
    amaslenn over 6 years
    You can also use git push -u origin HEAD
  • Todd
    Todd over 6 years
    @Stephane You only need the -u once to initiate tracking. Afterward just use git push
  • ErichBSchulz
    ErichBSchulz about 6 years
    @akronymn @ Federico - I've edited it to spell out what I see the dangers are - is that better?
  • piyushmandovra
    piyushmandovra almost 6 years
    dude bracket is just to mention you have to replace with whatever branch name you want to create and push.
  • Bruce Lee
    Bruce Lee over 5 years
    sometimes your need this git push origin -u local_branch:remote_branch
  • eli
    eli almost 5 years
    This is what exactly i was actively looking for
  • codeforester
    codeforester almost 5 years
    git throws error: src refspec <new branch> does not match any. when I try this.
  • Aditya Abhas
    Aditya Abhas almost 4 years
    This should be the top answer.
  • piyushmandovra
    piyushmandovra almost 4 years
    @AdiPrasetyo can you elaborate what you are trying to say/ask?
  • mss
    mss almost 4 years
    why does "git push origin -u remote_branch_name " work sometimes and sometimes not?
  • Darren Felton
    Darren Felton over 3 years
    @JonathanMoralesVélez changing the local branch is not necessary. You can push your local branch to any remote branch name like so: git push origin local-branch-name:remote-branch-name. Where origin is the name of the remote (often times actually "origin", local-branch-name is the actual name of your local branch name, and remote-branch-name is your desired branch name you want to push to.
  • Devin Rhode
    Devin Rhode over 3 years
    Edited answer to just have the one working link (github.com/DanielVartanov/willgit)
  • Dandgerson
    Dandgerson about 2 years
    the second variant is preferable
  • mannedear
    mannedear about 2 years
    @codeforester exactly the same error I'm facing now, I don't understand how to connect the local_branch to remote_branch in github, can someone help on this