获取现有的git分支来跟踪远程分支 [英] Getting existing git branches to track remote branches

查看:150
本文介绍了获取现有的git分支来跟踪远程分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b


  1. 创建本地存储库

  2. 在该资源库中执行一些工作,添加/更改文件等。

  3. 决定我需要一个中央远程存储库位置,并创建一个

  4. 将所有提交从本地存储库推送到此新的远程存储库

现在,我希望能够 push pull 从这个远程存储库中获取,而不必指定我要推送到哪里或从哪里提取;我希望我的本地主人跟踪远程主人。



正确做到这一点的方式对我来说并不清楚,而且我即使它不应该超过一个命令,也无法从文档中确定它。



因为这是每个存储库只做过一次的事情,我一般都会使用以下两种简单但却很冒险的解决方案之一:


  1. 使用 git clone 创建一个新的本地存储库,并删除旧的。在git克隆之后,新的存储库被设置为追踪原点。

  2. 手动编辑.git / config以创建主轨道原点。 b
    $ b

    我认为我应该能够运行一个命令,可能是某种形式的 git remote 来设置一个现有的存储库,让主跟踪远程主人。谁能告诉我该命令是什么?

    b $ b

      git branch --set-upstream local-branch-name origin / remote-branch-name 

    运行上面的命令可以正确地更新.git / config文件,甚至可以用这个输出验证:

    分支本地分支名称设置为从原点跟踪远程分支远程分支名称。

    编辑:正如 martijn 所说:在版本Git v1.8.0中,--set-upstream已被弃用,使用--set-upstream

      git branch --set-upstream-to local-branch-name origin / remote-branch-name 

    请参阅这个获取更多信息。


    My usual workflow when working with git, is something like this:

    1. create a local repository
    2. do some work in that repository, add/change files etc.
    3. decide that I want a central remote location for the repository, and create one
    4. push all the commits from my local repository to this new remote repository

    Now, however, I want to be able to push and pull from this remote repository without having to specify where I'm pushing to or pulling from; I want my local master to track the remote master.

    The proper way to do this isn't clear to me, and I've been unable to determine it from the documentation, even though it shouldn't really be more than one command.

    Because it's something that's only ever done once per repository, I've generally employed one of two simple, but hacky, solutions:

    1. used git clone to make a new local repository, and deleted the old one. After git cloning, the new repository is setup to track the origin.
    2. manually edited .git/config to make master track origin.

    I think I should be able to run a command, probably some form of git remote to setup an existing repository to have master track a remote master. Can anyone tell me what that command is?

    解决方案

    Use the set-upstream arg:

    git branch --set-upstream local-branch-name origin/remote-branch-name
    

    Running the above command updates your .git/config file correctly and even verifies with this output:

    "Branch local-branch-name set up to track remote branch remote-branch-name from origin."

    EDIT: As martijn said: "In version Git v1.8.0, --set-upstream is deprecated. Use --set-upstream-to instead."

    git branch --set-upstream-to local-branch-name origin/remote-branch-name
    

    See this for more information.

    这篇关于获取现有的git分支来跟踪远程分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆