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.
git push -u origin branch-name
. - anyone git pull
will often provide helpful messages about the appropriate command to set tracking information - anyone git branch --set-upstream-to origin/<branch>
- anyone --set-upstream
produces an error: fatal: the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead.
so git branch --set-upstream-to origin/<branch name>
is the current command that works. - anyone Given a branch foo
and a remote upstream
:
As of Git 1.8.0:
git branch -u upstream/foo
Or, if local branch foo
is not the current branch:
git branch -u upstream/foo foo
Or, if you like to type longer commands, these are equivalent to the above two:
git branch --set-upstream-to=upstream/foo
git branch --set-upstream-to=upstream/foo foo
As of Git 1.7.0 (before 1.8.0):
git branch --set-upstream foo upstream/foo
Notes:
foo
to track remote branch foo
from remote upstream
.git fetch upstream
beforehand.See also: Why do I need to do `--set-upstream` all the time?
Answered 2023-09-20 20:13:21
git branch --set-upstream master origin/master
would be equivalent to what is automatically done when you initially clone a repository. - anyone git push -u origin foo
via - anyone 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
git remote add origin ssh://...
Now configure master to know to track:
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
And push:
git push origin master
Answered 2023-09-20 20:13:21
error: the requested upstream branch 'upstream/master' does not exist
. - anyone origin
and not upstream
. - anyone I do this as a side-effect of pushing with the -u
option as in
$ git push -u origin branch-name
The equivalent long option is --set-upstream
.
The git-branch
command also understands --set-upstream
, but its use can be confusing. Version 1.8.0 modifies the interface.
git branch --set-upstream
is deprecated and may be removed in a relatively distant future.git branch [-u|--set-upstream-to]
has been introduced with a saner order of arguments.…
It was tempting to say
git branch --set-upstream origin/master
, but that tells Git to arrange the local branch "origin/master" to integrate with the currently checked-out branch, which is highly unlikely to be what the user meant. The option is deprecated; use the new--set-upstream-to
(with a short-and-sweet-u
) option instead.
Say you have a local foo
branch checked out and want it to use a branch by the same name as its upstream. Make this happen with
$ git branch --set-upstream-to=origin/foo
Answered 2023-09-20 20:13:21
For Git versions 1.8.0 and higher:
Actually for the accepted answer to work:
git remote add upstream <remote-url>
git fetch upstream
git branch -f --track qa upstream/qa
# OR Git version 1.8.0 and higher:
git branch --set-upstream-to=upstream/qa
# Gitversions lower than 1.8.0
git branch --set-upstream qa upstream/qa
Answered 2023-09-20 20:13:21
You might find the git_remote_branch
tool useful. It offers simple commands for creating, publishing, deleting, tracking & renaming remote branches. One nice feature is that you can ask a grb
command to explain what git commands it would execute.
grb explain create my_branch github
# git_remote_branch version 0.3.0
# List of operations to do to create a new remote branch and track it locally:
git push github master:refs/heads/my_branch
git fetch github
git branch --track my_branch github/my_branch
git checkout my_branch
Answered 2023-09-20 20:13:21
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:
Answered 2023-09-20 20:13:21
I believe that in as early as Git 1.5.x you could make a local branch $BRANCH
track a remote branch origin/$BRANCH
, like this.
Given that $BRANCH
and origin/$BRANCH
exist, and you've not currently checked out $BRANCH
(switch away if you have), do:
git branch -f --track $BRANCH origin/$BRANCH
This recreates $BRANCH
as a tracking branch. The -f
forces the creation despite $BRANCH
existing already. --track
is optional if the usual defaults are in place (that is, the git-config parameter branch.autosetupmerge
is true).
Note, if origin/$BRANCH
doesn't exist yet, you can create it by pushing your local $BRANCH
into the remote repository with:
git push origin $BRANCH
Followed by the previous command to promote the local branch into a tracking branch.
Answered 2023-09-20 20:13:21
git push origin $BRANCH
was what I was looking for. - anyone git branch -f --track $BRANCH origin/$BRANCH
does the trick. - anyone Make sure you run :
git config push.default tracking
to be able to push trouble free
Answered 2023-09-20 20:13:21
git-config(1)
manual page, tracking
is deprecated synonym of upstream
. - anyone git checkout -b mybranch origin/master
, and making changes on your local branch, the above will push to master on the remote when you run git push
. For most people this would be unexpected. - anyone Editing .git/config
is probably the easiest and fastest way. That's what the Git commands for handling remote branches are doing, anyway.
If you don't want to muck with the file by hand (and it's not that hard to do), you can always use git config
to do it...but again, that's just going to edit the .git/config
file, anyway.
There are, of course, ways to automatically track a remote branch when using git checkout
(by passing the --track
flag, for example), but these commands work with new branches, not existing ones.
Answered 2023-09-20 20:13:21
In very short
git branch --set-upstream yourLocalBranchName origin/develop
This will make your yourLocalBranchName
track the remote branch called develop
.
Answered 2023-09-20 20:13:21
git push -u origin branch
(or --set-upstream-to
) instead - anyone --set-upstream
and --track
? I don't quite understand why I should use one over the other. - anyone For 1.6.x, it can be done using the git_remote_branch tool:
grb track foo upstream
That will cause Git to make foo
track upstream/foo
.
Answered 2023-09-20 20:13:21
I use the following command (Suppose your local branch name is "branch-name-local" and remote branch name is "branch-name-remote"):
$ git branch --set-upstream-to=origin/branch-name-remote branch-name-local
If both local and remote branches have the same name, then just do the following:
$ git branch --set-upstream-to=origin/branch-name branch-name
Answered 2023-09-20 20:13:21
For creating new branch, we could use following command
git checkout --track -b example origin/exampleFor the already created branch to create link between remote then from that branch use below command
git branch -u origin/remote-branch-name
Answered 2023-09-20 20:13:21
After a git pull
:
git checkout --track <remote-branch-name>
Or:
git fetch && git checkout <branch-name>
Answered 2023-09-20 20:13:21
Here, using github
and git version 2.1.4
, just do:
$ git clone git@github.com:user/repo.git
And remotes come by itelsef, even if not linked locally:
$ git remote show origin
* remote origin
Fetch URL: git@github.com:user/repo.git
Push URL: git@github.com:user/repo.git
HEAD branch: master
Remote branches:
develop tracked <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
But of course, still no local branch:
$ git branch
* master <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
See? Now if you just checkout develp, it will do the magic automatically:
$ git checkout develop
Branch develop set up to track remote branch develop from origin.
Switched to a new branch 'develop'
So easy!
Summary. Just run this 2 commands:
$ git clone git@github.com:user/repo.git
$ git checkout develop
Answered 2023-09-20 20:13:21
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
Answered 2023-09-20 20:13:21
For git version 2.25.1
, use the command:
git push --set-upstream origin <local_branch_name>
Answered 2023-09-20 20:13:21
In case you got "error: the requested upstream branch 'origin/foo' does not exist" after running:
git branch -u origin/foo
Make sure origin
does have a foo
branch.
Make sure the remote.origin.fetch
variable is set to +refs/heads/*:refs/remotes/origin/*
:
$ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
$ git config --get remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
git fetch -v
. You should see git updating from origin/foo
:$ git fetch -v
From github.com:davidhcefx/test
* [new branch] foo -> origin/foo
= [up to date] master -> origin/master
git branch -avv
will show square brackets, indicating tracked remote branches:$ git branch -u origin/foo
branch 'foo' set up to track 'origin/foo'.
$ git branch -avv
* foo 92c5ada [origin/foo] Initial commit
master 92c5ada [origin/master] Initial commit
Answered 2023-09-20 20:13:21
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: the requested upstream branch 'origin/remotebranchname' does not exist
).
In the end I managed to add the new, previously unknown remote branch (without fetching) by adding a new head file at .git/refs/remotes/origin/remotebranchname
and then copying the ref (eyeballing was quickest, lame as it was ;-) from the system with access to the origin repo to the workstation (with the local repo where I was adding the remote branch on).
Once that was done, I could then use git branch --set-upstream-to=origin/remotebranchname
Answered 2023-09-20 20:13:21
or simply by :
switch to the branch if you are not in it already:
[za]$ git checkout branch_name
run
[za]$ git branch --set-upstream origin branch_name
Branch origin set up to track local branch brnach_name by rebasing.
and you ready to :
[za]$ git push origin branch_name
You can alawys take a look at the config file to see what is tracking what by running:
[za]$ git config -e
It's also nice to know this, it shows which branches are tracked and which ones are not. :
[za]$ git remote show origin
Answered 2023-09-20 20:13:21
For anyone who, like me, just wants to sync up your local branch name with the remote branch name, here's a handy command:
git branch -u origin/$(git rev-parse --abbrev-ref HEAD)
Answered 2023-09-20 20:13:21
To avoid remembering what you need to do each time you get the message:
Please specify which branch you want to merge with. See git-pull(1)
for details.
.....
You can use the following script which sets origin as upstream for the current branch you are in.
In my case I almost never set something else than origin as the default upstream. Also I almost always keep the same branch name for local and remote branch. So the following fits me:
#!/bin/bash
# scriptname: git-branch-set-originupstream
current_branch="$(git branch | grep -oP '(?<=^\* )(.*)$')"
upstream="origin/$current_branch"
git branch -u "$upstream"
Answered 2023-09-20 20:13:21
git config --global branch.autoSetupMerge simple
and git config --global push.autoSetupRemote true
. At checkout you will get tracking only if the local branch name matches the remote, and at git push
time a new local branch will create the new remote branch, with tracking, automatically. - anyone This would work too
git branch --set-upstream-to=/< remote>/< branch> < localbranch>
Answered 2023-09-20 20:13:21