I know how to make a new branch that tracks remote branches, but how do I make an existing branch track a remote branch?
I know I can just edit the .git/config
file, but it seems there should be an easier way.
I know how to make a new branch that tracks remote branches, but how do I make an existing branch track a remote branch? I know I can just edit the |
||||
Given a branch As of Git 1.8.0:
Or, if local branch
Or, if you like to type longer commands, these are equivalent to the above two:
As of Git 1.7.0:
Notes: All of the above commands will cause local branch See also: Why do I need to do `--set-upstream` all the time? |
|||||||||||||||||||||
|
You can do the following (assuming you are checked out on master and want to push to a remote branch master): Set up the 'remote' if you don't have it already
Now configure master to know to track:
And push:
|
|||||||||||||||||||||
|
I do this as a side-effect of pushing with the
The equivalent long option is The
Say you have a local foo branch and want it to treat the branch by the same name as its upstream. Make this happen with
or just
|
|||
|
You might find the
|
|||||||||||||
|
Actually for the accepted answer to work:
|
|||||||||
|
I believe that in as early as Git 1.5.x you could make a local branch Given that
This recreates Note, if
Followed by the previous command to promote the local branch into a tracking branch. |
|||||||||
|
Editing If you don't want to muck with the file by hand (and it's not that hard to do), you can always use There are, of course, ways to automatically track a remote branch when using |
|||
|
Make sure you run :
to be able to push trouble free |
|||||
|
In very short
This will make your |
|||||||||
|
For 1.6.x, it can be done using the git_remote_branch tool:
That will cause Git to make |
||||
|
1- update your local meta-data using : git fetch --all 2- show your remote and local branches using : git branch -a , see the following Screenshot 3- switch to target branch , that you want to linked with the remote: using git checkout branchName example : 4- Link your local branch to a remote branch using: git branch --set-upstream-to nameOfRemoteBranch N.B : nameOfRemoteBranch : to copy from the output of step 2 " git branch -r " Example of use: |
||||
|
In a somewhat related way I was trying to add a remote tracking branch to an existing branch, but did not have access to that remote repository on the system where I wanted to add that remote tracking branch on (because I frequently export a copy of this repo via sneakernet to another system that has the access to push to that remote). I found that there was no way to force adding a remote branch on the local that hadn't been fetched yet (so local did not know that the branch existed on the remote and I would get the error: In the end I managed to add the new, previously unknown remote branch (without fetching) by adding a new head file at Once that was done, I could then use |
||||
|
Here, using
And remotes come by itelsef, even if not linked locally:
But of course, still no local branch:
See? Now if you just checkout develp, it will do the magic automatically:
So easy! Summary. Just run this 2 commands:
|
|||
|
I use the following command (Suppose yout branch name is "branch-name"):
|
||||
|
Simply, after a
Or,
|
||||
|
This isn't a direct answer to this question, but I wanted to leave a note here for anyone who may be having the same issue as me when trying to configure an upstream branch. Be wary of push.default. With older git versions, the default was matching, which would cause very undesirable behaviour if you have, for example: Local branch "master" tracking to origin/master Remote branch "upstream" tracking to upstream/master If you tried to "git push" when on the "upstream" branch, with push.default matching git would automatically try to merge the local branch "master" into "upstream/master", causing a whole lot of chaos. This gives more sane behaviour: git config --global push.default upstream |
|||
|
This would work too
|
||||
|
or simply by : switch to the branch if you are not in it already:
run
and you ready to :
You can alawys take a look at the config file to see what is tracking what by running:
|
|||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
git push -u origin branch-name
. – Zags Mar 6 '14 at 23:46git branch
documentation. – user456814 May 23 '14 at 18:26git pull
will often provide helpful messages about the appropriate command to set tracking information – billrichards Aug 7 '15 at 12:59