How do I change the remote a git 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

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


For me the fix was:

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

Then:

git push

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?

链接地址: http://www.djcxy.com/p/4542.html

上一篇: 推送当前分支快捷键

下一篇: 如何更改git分支正在跟踪的远程?