How do I push a new local branch to a remote Git repository and track it too?
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:
-
Create a local branch based on some other (remote or local) branch:
git checkout -b branchname
-
Push the local branch to the remote repository (publish), but make it trackable so
git pull
andgit push
will work immediatelygit 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:
Roni Yaniv
Updated on July 11, 2022Comments
-
Roni Yaniv almost 2 years
I want to be able to do the following:
Create a local branch based on some other (remote or local) branch (via
git branch
orgit checkout -b
)Push the local branch to the remote repository (publish), but make it trackable so
git pull
andgit 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 over 10 yearsjust to point out --set-upstream is -u
-
Admin over 3 yearslots 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 over 3 years@VividD "added an illustrative picture" - Seriously? o.O
-
Kip about 2 yearsmy 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 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 about 14 yearsWilliam's script I linked to does about the same with the additional option to delete remote branches and some safeguards, too
-
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 about 14 yearsThis 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 about 14 yearsthis assumes I have ruby installed. no such luck. any other ideas?
-
Roni Yaniv about 14 yearsseems kind of overkill. does the
git remote add origin
make the local branch trackable? is that the key command here? -
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 about 14 yearswell - 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 about 14 yearsthe ruby script calls
git push
andgit 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 almost 10 yearsIt'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 toupstream
, 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 dogit branch --unset-upstream
first. -
Paul Whipp almost 10 yearsI 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 over 9 yearsand
git pull --all
pulls it all back elsewhere ? kewl -
Mike D over 9 yearsWilliam's miscellaneous git tools appears to have moved (that link is now dead). A working link is: gitorious.org/willgit
-
amey91 over 9 yearsThis command sets up tracking to the correct branch without the need to push anything. Thank you.
-
Puterdo Borato about 9 yearsFor 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 almost 9 yearsFor those still confused about the concept I like to "THINK" of it that
git push -u origin myepicbranchname
is equal togit 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 about 8 yearsIn
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 itoldbranch
, and want to push to anewbranch
, the command will be likegit push origin oldbranch:newbranch
. -
Federico Razzoli almost 8 yearsGit 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 over 7 years"William's" link broken again; new link seems to be git-wt-commit.rubyforge.org
-
Robert over 7 years
git branch <branch-name>
andgit checkout -b <branch-name>
both create a branch but checkout switch to the new branch -
Daniel Tonon over 7 yearsThank you :)
git push -u origin <branch-name>
wasn't working for me but usingHEAD
instead of<branch-name>
worked perfectly :) -
JonTheNiceGuy over 7 yearsSomewhat annoyingly, I found that
git push -u origin branchname
wasn't working, but thatgit push -u --set-upstream origin branchname
did. This is with the PoshGit scripts provided with Github for Windows. -
akronymn over 7 yearsThis 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 almost 7 years@Federico @akronymn Where can one find the dangers of doing
git push --all -u
? -
Jonathan Morales Vélez over 6 yearsPlease 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 sayserror: src refspec new-named-branch does not match any.
The fix is to rungit branch -m new-named-branch
and then runninggit push origin new-named-branch
. -
amaslenn over 6 yearsYou can also use
git push -u origin HEAD
-
Todd over 6 years@Stephane You only need the
-u
once to initiate tracking. Afterward just usegit push
-
ErichBSchulz about 6 years@akronymn @ Federico - I've edited it to spell out what I see the dangers are - is that better?
-
piyushmandovra almost 6 yearsdude bracket is just to mention you have to replace with whatever branch name you want to create and push.
-
Bruce Lee over 5 yearssometimes your need this
git push origin -u local_branch:remote_branch
-
eli almost 5 yearsThis is what exactly i was actively looking for
-
codeforester almost 5 yearsgit throws
error: src refspec <new branch> does not match any.
when I try this. -
Aditya Abhas almost 4 yearsThis should be the top answer.
-
piyushmandovra almost 4 years@AdiPrasetyo can you elaborate what you are trying to say/ask?
-
mss almost 4 yearswhy does "git push origin -u remote_branch_name " work sometimes and sometimes not?
-
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
. Whereorigin
is the name of the remote (often times actually "origin",local-branch-name
is the actual name of your local branch name, andremote-branch-name
is your desired branch name you want to push to. -
Devin Rhode over 3 yearsEdited answer to just have the one working link (github.com/DanielVartanov/willgit)
-
Dandgerson about 2 yearsthe second variant is preferable
-
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