git 如何更改分支跟踪的远程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4878249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the remote a branch is tracking?
提问by joachim
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.
该central
库必须建立一个新的服务器上,所以我创建了我的本地回购新的远程,并推到这一点。
But now when I do git pull
, it claims I am up to date. It's wrong—it's telling me about the oldremote branch, not the new one, which I know for a fact has new commits to fetch.
但是现在当我这样做时git pull
,它声称我是最新的。这是错误的——它告诉我的是旧的远程分支,而不是新的,我知道它有新的提交要获取。
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.
我可以在 git 配置文件中看到这一点,但我不想把事情搞砸。
[branch "master"]
remote = oldserver
merge = refs/heads/master
回答by urschrei
Using git v1.8.0or later:
使用git v1.8.0或更高版本:
git branch branch_name
--set-upstream-to
your_new_remote/branch_name
git branch branch_name
--set-upstream-to
your_new_remote/branch_name
Or you can use the -u
switch:
或者您可以使用-u
开关:
git branch branch_name
-u
your_new_remote/branch_name
git branch branch_name
-u
your_new_remote/branch_name
Using git v1.7.12or earlier:
使用 git v1.7.12或更早版本:
git branch --set-upstream branch_name your_new_remote/branch_name
git branch --set-upstream branch_name your_new_remote/branch_name
回答by critikaster
For me the fix was:
对我来说,修复是:
git remote set-url origin https://some_url/some_repo
Then:
然后:
git push
回答by Ko2r
With an up to date git (2.5.5)the command is the following :
使用最新的git (2.5.5)命令如下:
git branch --set-upstream-to=origin/branch
This will update the remote tracked branch for your current local branch
这将更新当前本地分支的远程跟踪分支
回答by wranvaud
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.
然后随意编辑文件,保存并应用您的修改。
回答by Cascabel
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?
也就是说,如果新服务器将成为规范的远程服务器,为什么不称其为 origin,就像您最初从它克隆一样?
回答by uma
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
回答by Andries
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:
例如,给定命令git remote -v
产生如下内容:
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
回答by RDL
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
或者将更改远程服务器更改为配置中的当前服务器
回答by Arshan Khanifar
Based on what I understand from the latest git documentation, the synopsis is:
根据我从最新的 git文档中了解到的,概要是:
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:
这种用法似乎与 urschrei 的回答有点不同,因为在他的概要中是:
git branch local-branch -u upstream-branch
git branch local-branch --set-upstream-to=upstream-branch
I'm guessing they changed the documentation again?
我猜他们又更改了文档?
回答by Mohideen bin Mohammed
In latest git version like 2.7.4,
在最新的 git 版本中,如2.7.4,
git checkout branch_name
#branch name which you want to change tracking branch
git checkout branch_name
#要更改跟踪分支的分支名称
git branch --set-upstream-to=upstream/tracking_branch_name
#upstream - remote name
git branch --set-upstream-to=upstream/tracking_branch_name
#upstream - 远程名称