中央存储库必须设置在一个新的服务器上,所以我在本地回购上创建了一个新的远程存储库,并将其推送到该服务器上。

但现在当我做git拉,它声称我是最新的。这是错误的——它告诉我的是旧的远程分支,而不是新分支,我知道新分支实际上有新的提交要获取。

如何更改本地分支以跟踪不同的远程?

我可以在git配置文件中看到这一点,但我不想把事情搞砸。

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

当前回答

使用git v1.8.0或更高版本:

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

或者你可以使用-u开关

git branch branch_name -u your_new_remote/branch_name

使用git v1.7.12或更早版本

git branch --set-upstream branch_name your_new_remote/branch_name

其他回答

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

如果你头脑清醒的话,编辑配置文件是很安全的。如果你想要更偏执一点,你可以使用瓷器命令来修改它:

git config branch.master.remote newserver

当然,如果您查看之前和之后的配置,您将看到它完全按照您要做的做了。

但在你的个人情况下,我要做的是:

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

也就是说,如果新服务器将成为规范的远程服务器,为什么不将其称为origin,就像您最初从它克隆出来一样?

你可以删除你当前的分支,然后这样做:

git branch --track local_branch remote_branch

或者在配置中将远程服务器更改为当前服务器

对我来说,解决办法是:

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

然后:

git push

根据我从最新的git文档中了解到的内容,概要如下:

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

这种用法似乎与urschrei的回答有点不同,因为在他的简介中是:

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

我猜他们又修改了文件?