987

The central repository had to be set up on a new server, so I created a new remote on my local repo, and pushed to that.

But now when I do git pull, it claims I am up to date. It's wrong—it's telling me about the old remote branch, not the new one, which I know for a fact has new commits to fetch.

How do I change my local branch to track a different remote?

I can see this in the git config file but I don't want to mess things up.

[branch "master"]
    remote = oldserver
    merge = refs/heads/master
4

15 Answers 15

1577

Using git v1.8.0 or later:

git branch branch_name --set-upstream-to your_new_remote/branch_name

Or you can use the -u switch

git branch branch_name -u your_new_remote/branch_name

Using git v1.7.12 or earlier

git branch --set-upstream branch_name your_new_remote/branch_name
10
  • 7
    Ah, my bad. I'd still do it via config, because you can be sure to not accidentally change the branch name, but all good. +1.
    – Cascabel
    Feb 4, 2011 at 8:23
  • From the 1.8.0 release notes: "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 what the user meant. The option is deprecated; use the new "--set-upstream-to" (with a short-and-sweet "-u") option instead."
    – Jared Beck
    Oct 30, 2012 at 22:20
  • 122
    If the remote tracking branch does not yet exists but an existing remote tracking branch is currently tracked, it can not be changed to the in-existent new remote tracking branch. You need to unset the current remote tracking branch first: git branch --unset-upstream - then it works as given in this answer (or with the next git push -u / --set-upstream).
    – hakre
    Apr 12, 2015 at 10:53
  • 2
    Thanks! This works well! Btw, if anyone gets this error: error: the requested upstream branch 'origin/master' does not exist then simply run git fetch to retrieve info about the remote. I renamed origin to something else and added a new origin, but I hadn't fetched info about it.
    – racl101
    Jan 25, 2017 at 5:04
  • 1
    git branch branch_name --set-upstream-to origin/branch_name what was I needed, cheers.
    – Leo
    Dec 16, 2020 at 12:00
133

My original answer was:

For me the fix was:

git remote set-url origin https://some_url/some_repo

Then:

git push

A better answer is:

Looking back at this answer a while later, and as @stevendesu correctly points out in the comment, the better way would be:

Remove the old origin reference:

git remote remove origin

Add a new origin reference:

git remote add origin https://some_url/some_repo

Verify this worked with:

git remote -v

And/or:

git remote show origin

Also note that you can add multiple origins, for instance, you can leave the origin remote intact, and add one called with another name, for instance, secondary_repo, with:

git remote add secondary_repo https://some_url/some_repo

You will now see multiple repos with git remote -v, something like:

origin  https://some_url/some_origin_repo (fetch)
origin  https://some_url/some_origin_repo (push)
secondary_repo  https://some_url/some_repo (fetch)
secondary_repo  https://some_url/some_repo (push)

However, make sure you always specify which repo name your pulling from/pushing to, like so:

git push origin

or:

git push secondary_repo

Also see the docs for more details.

2
  • 3
    This is the way Github says to do it. help.github.com/articles/changing-a-remote-s-url
    – fsharp
    Sep 28, 2015 at 19:21
  • 28
    This actually changes where the remote is, it doesn't change which remote you are tracking (technically) -- you are still tracking "origin", it just updates the definition of origin. For some use cases this could be a bad thing. Suppose you fork jQuery and make a new remote. "origin" is your fork, "upstream" is jQuery. Then suppose you need to temporarily track the original repo instead of your fork. If you used set-url and forgot to set it back, then future git push commands would fail
    – stevendesu
    Feb 3, 2016 at 22:17
105

With an up to date git (2.5.5) the command is the following :

git branch --set-upstream-to=origin/branch

This will update the remote tracked branch for your current local branch

3
  • 3
    Hitting git branch --set-upstream-to=origin/develop raised the error: error: the requested upstream branch 'origin/develop' does not exist. Previously, I renamed a remote branch to delelop.
    – belgoros
    Feb 9, 2018 at 13:30
  • 3
    @belgoros git fetch --all maybe ? Apr 4, 2019 at 8:30
  • 1
    @belgoros, I ran into the same problem. Sovled it by using git push --set-upstream origin instead. Only after that, I realised @hakre's comment on the accepted answer was explaining it more clearly already.
    – rijam
    Jul 7, 2021 at 10:00
32

Another option to have a lot of control over what's happening is to edit your configurations by hand:

git config --edit

or the shorthand

git config -e

Then edit the file at will, save and your modifications will be applied.

1
  • 1
    My issue was that there were two origins. (origin and origin-algo). But the local master was tracking "origin-algo". But I wanted it to track "origin". The only way I could change that was by editing the git config. Apr 27, 2020 at 22:41
21

If you're sane about it, editing the config file's safe enough. If you want to be a little more paranoid, you can use the porcelain command to modify it:

git config branch.master.remote newserver

Of course, if you look at the config before and after, you'll see that it did exactly what you were going to do.

But in your individual case, what I'd do is:

git remote rename origin old-origin
git remote rename new-origin origin

That is, if the new server is going to be the canonical remote, why not call it origin as if you'd originally cloned from it?

3
  • 3
    I had actually done that before encoutering this problem -- git was clever and carried the remote rename through to the config file, so in your example, my config file said 'old-origin'.
    – joachim
    Feb 4, 2011 at 7:21
  • Personally, I think this way makes more sense conceptually than the accepted way, but I guess they are functionally equivalent, correct? Apr 25, 2012 at 17:45
  • 1
    @Jefromi: A remote rename would not do what is being asked for as it changes the remote's name both in [remote] configs and in [branch] configs. So what one needs to do in this case is to edit the config file and do what you are saying (renaming of remotes) just at the [remote] config lines. Mar 8, 2013 at 6:42
14

This is the easiest command:

git push --set-upstream <new-origin> <branch-to-track>

For example, given the command git remote -v produces something like:

origin  ssh://[email protected]/~myself/projectr.git (fetch)
origin  ssh://[email protected]/~myself/projectr.git (push)
team    ssh://[email protected]/vbs/projectr.git (fetch)
team    ssh://[email protected]/vbs/projectr.git (push)

To change to tracking the team instead:

git push --set-upstream team master
1
  • The branch to track is optional when there is nothing ambiguous. I used your suggestion when moving away from Github ;) Thank you.
    – gouessej
    Aug 12, 2019 at 20:28
7
git fetch origin
git checkout --track -b local_branch_name origin/branch_name

or

git fetch
git checkout -b local_branch_name origin/branch_name
1
  • For my case I needed git fetch. I had created a new branch in bitbucket and wanted to switch to it. git doesn't know about remote changes so I had to do git fetch before git checkout feature/new-feature-branch. Hope this helps someone else. Apr 16, 2019 at 3:46
4

You could either delete your current branch and do:

git branch --track local_branch remote_branch

Or change change remote server to the current one in the config

1
  • didn't work for me, only working command was the one from @uma : git checkout --track -b local_branch_name origin/branch_name Dec 17, 2015 at 9:00
3

Based on what I understand from the latest git documentation, the synopsis is:

git branch -u upstream-branch local-branch
git branch --set-upstream-to=upstream-branch local-branch

This usage seems to be a bit different than urschrei's answer, as in his the synopsis is:

git branch local-branch -u upstream-branch 
git branch local-branch --set-upstream-to=upstream-branch 

I'm guessing they changed the documentation again?

3

the easiest way is to simply push to the new branch:

git push -u origin branch/name

1
  • 1
    Not sure why this answer isn't getting more love - Its literally the quickest way to update which remote is being tracked. I came looking for a better answer than this , i.e. a command line option that would change without pushing, but this seems to be the best fit ... git config -e is a second option if you're not ready to push ... Jan 8, 2022 at 0:16
2

I've found @critikaster's post helpful, except that I had to perform these commands with GIT 2.21:

$ git remote set-url origin https://some_url/some_repo
$ git push --set-upstream origin master
2

Based on the git documentation the best way is:

  1. be sure the actual origin path:

git remote -v

  1. Then make the change with:

git remote set-url origin

where url-repository is the same URL that we get from the clone option.

1
  • 1
    The question is about changing the pointer to a remote tracking branch, not a remote's URL. Also, there already are 2 remotes with different URLs, so this suggestion only misconfigure one remote. Further, when mentioning documentation, be sure to link to the page in question and to quote the relevant parts. Lastly, there seem to be many valid answers to this question already (with one being very similar to yours in fact), so unless you found something novel, I'd suggest just improving the existing answers.
    – kelvin
    Oct 19, 2020 at 21:56
1

In latest git version like 2.7.4,

git checkout branch_name #branch name which you want to change tracking branch

git branch --set-upstream-to=upstream/tracking_branch_name #upstream - remote name

0

After trying the above and searching, searching, etc. I realized none of my changes were on the server that were on my local branch and Visual Studio in Team Explorer did not indicate this branch tracked a remote branch. The remote branch was there, so it should have worked. I ended up deleting the remote branch on github and 're' Push my local branch that had my changes that were not being tracked for an unknown reason.

By deleting the remote branch and 're' Push my local branch that was not being tracked, the local branch was re-created on git hub. I tried to this at the command prompt (using Windows) I could not get my local branch to track the remote branch until I did this. Everything is back to normal.

1
  • 1
    Hi Keenan. Please refrain from adding signatures to your material here - it is thought that the profile card performs this task sufficiently well. Thanks!
    – halfer
    Aug 22, 2020 at 10:18
-3

I tried lots of solution but this one worked for me from Bitbucket to Azure Devops Migration:

  1. Create Repository
  2. git clone Source URL
  3. git config --global --unset credential.helper
  4. git config credential.helper store
  5. git remote rm origin
  6. git remote add origin URL to NEW repo
  7. git push origin --all
    In case error use
    git push -f origin --all
    In case error in access or identity
    go to branch-->select branch->3 dots-->branch security --> allow force push
  8. git push --tags

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.