首页 文章

如何更改远程分支正在跟踪?

提问于
浏览
484

必须在新服务器上设置 central 存储库,因此我在本地存储库上创建了一个新的远程数据库,然后将其推送到该服务器 .

但现在当我做 git pull 时,它声称我是最新的 . 它's wrong—it'告诉我有关旧的远程分支,而不是新的远程分支,我知道有一个事实有新的提交提取 .

如何更改本地分支以跟踪不同的遥控器?

我可以在git配置文件中看到这个,但我不想搞砸了 .

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

10 回答

  • 17

    使用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

  • 2

    对我来说,修复是:

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

    然后:

    git push
    
  • 6

    使用最新的git(2.5.5)命令如下:

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

    这将更新当前本地分支的远程跟踪分支

  • 16

    如果你对它有所了解,编辑配置文件足够安全 . 如果你想变得更偏执,你可以使用瓷器命令来修改它:

    git config branch.master.remote newserver
    

    当然,如果您在之前和之后查看配置,您会发现它确实完成了您要执行的操作 .

    但在你的个案中,我要做的是:

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

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

  • 68

    对发生的事情进行大量控制的另一个选择是手动编辑配置:

    git config --edit
    

    或速记

    git config -e
    

    然后随意编辑文件,保存并应用您的修改 .

  • 19
    git fetch origin
    git checkout --track -b local_branch_name origin/branch_name
    

    要么

    git fetch
    git checkout -b local_branch_name origin/branch_name
    
  • 4

    您可以删除当前分支并执行以下操作:

    git branch --track local_branch remote_branch
    

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

  • 788

    基于我从最新的git documentation中理解的内容,概要是:

    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
    

    我猜他们又改变了文档?

  • 1

    这是最简单的命令:

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

    例如,给定命令 git remote -v 产生类似于:

    origin  ssh://git@bitbucket.some.corp/~myself/projectr.git (fetch)
    origin  ssh://git@bitbucket.some.corp/~myself/projectr.git (push)
    team    ssh://git@bitbucket.some.corp/vbs/projectr.git (fetch)
    team    ssh://git@bitbucket.some.corp/vbs/projectr.git (push)
    

    改为跟踪团队:

    git push --set-upstream team master
    
  • 2

    在像 2.7.4 这样的最新git版本中,

    git checkout branch_name #branch要更改跟踪分支的名称

    git branch --set-upstream-to=upstream/tracking_branch_name #upstream - 远程名称

相关问题