joachim
joachim

Reputation: 30761

How to change the remote a branch is tracking?

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

Upvotes: 1052

Views: 872335

Answers (15)

Raoul
Raoul

Reputation: 2212

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.

Upvotes: 142

Yunus
Yunus

Reputation: 1

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

Upvotes: -4

urschrei
urschrei

Reputation: 26859

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

Upvotes: 1680

mtwom
mtwom

Reputation: 37

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.

Upvotes: 2

Keenan Stewart
Keenan Stewart

Reputation: 634

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.

Upvotes: 0

ArthNRick
ArthNRick

Reputation: 935

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

git push -u origin branch/name

Upvotes: 4

Ko2r
Ko2r

Reputation: 1631

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

Upvotes: 114

user8128167
user8128167

Reputation: 7676

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

Upvotes: 2

Mohideen bin Mohammed
Mohideen bin Mohammed

Reputation: 20137

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

Upvotes: 1

Andries
Andries

Reputation: 317

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

Upvotes: 15

Arshan Khanifar
Arshan Khanifar

Reputation: 301

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?

Upvotes: 3

William Ranvaud
William Ranvaud

Reputation: 1093

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.

Upvotes: 33

uma
uma

Reputation: 2952

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

Upvotes: 7

RDL
RDL

Reputation: 7961

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

Upvotes: 6

Cascabel
Cascabel

Reputation: 496722

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?

Upvotes: 21

Related Questions