获取现有的git分支来跟踪远程分支


81

使用git时,我通常的工作流程是这样的:

  1. 创建本地存储库
  2. 在该存储库中做一些工作,添加/更改文件等。
  3. 决定我想要存储库的中央远程位置,然后创建一个
  4. 将所有提交从我的本地存储库推送到这个新的远程存储库

然而,现在我希望能够pushpull从该远程存储库,而无需指定我推或拉离; 我希望我的本地主机跟踪远程主机。

对我来说,执行此操作的正确方法尚不明确,尽管它实际上不应只是一个命令,但我无法从文档中确定它。

因为每个存储库只能完成一次,所以我通常采用以下两种简单但棘手的解决方案之一:

  1. 用于git clone建立新的本地存储库,并删除了旧的存储库。git克隆后,将建立新的存储库以跟踪源。
  2. 手动编辑.git / config以使主音轨起源。

我认为我应该能够运行命令,可能是某种形式的命令,git remote以设置现有存储库以使母版跟踪远程母版。谁能告诉我该命令是什么?


实际上,以前曾经问过类似的问题:stackoverflow.com/questions/312055/…–
SpoonMeiser

对于git version> = 1.7.0.0,您可以在一个命令中进行操作,此处进行了说明。
KiRPiCH 2010年

4
这实际上是一个重复:stackoverflow.com/questions/520650/...
takeshin

Answers:


155

使用set-upstream arg:

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

运行上面的命令可以正确更新.git / config文件,甚至可以通过以下输出进行验证:

“分支本地分支名称已设置为从源开始跟踪远程分支远程分支名称。”

编辑:martijn所说:“在版本Git v1.8.0中,不建议使用--set-upstream。请改用--set-upstream-to。”

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

请参阅以获取更多信息。


18
在版本Git v1.8.0中,--set-upstream已弃用。使用--set-upstream-to代替。
Martijn

1
使用git remote show <remote>查看本地分支机构跟踪的分支机构
naitsirch 2013年

1
建议的编辑不应该被批准,它从根本上改变了答案(并且由于新选项的参数相反,因此将其置于错误的状态)。让原始海报决定他是否要更新他的答案。

1
您也可以通过git push -u
mattr-

是否set upstream在没有实际推入任何代码的情况下设置映射?push强制您推送代码以创建映射。也许您还没有代码,但是想建立一个映射?
user20358'6

19

git help remote应该告诉您您需要知道的内容。我想你想要的是

git remote add [remote-name] [remote-url]

# Set a local branch to follow the remote
git config branch.[branch-name].remote [remote-name]

# Set it to automatically merge with a specific remote branch when you pull
git config branch.[branch-name].merge [remote-master]

您也可以手动编辑.git / config进行设置。


另一个选择是创建中央存储库,然后删除您的工作目录并通过从中央存储库进行克隆来重新创建它。不过,编辑.git / config会提供更多信息。
William Pursell 09年

克隆中央存储库并编辑.git / config是我在问题中已经提到的两个选项。
SpoonMeiser

8

如果要创建新的本地分支来跟踪远程分支,也可以使用此命令:

git checkout --track -b [branch_name] --track origin[or other remote name]/[remote_branch_name] 

甚至更好:

git checkout -t origin/branch_name

git checkout --track -b分支名称起点分支名称致命:git checkout:更新路径与切换分支不兼容。您是否打算签出无法解析为提交的“来源/分支名称”?
edebill 2010年

6

在较新版本的git上,您可以使用

git branch --track origin/branch_name

4
由于帖子太短/只有代码,因此该帖子被自动标记为低质量。您介意通过添加一些文字来解释它如何解决问题来扩展它吗?
gung-恢复莫妮卡2014年

这是最准确的答案!以上所有内容都不正确或不再受支持git branch --set-upstream BRANCH fatal: the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead.
-GTodorov

3

The --set-upstream flag is deprecated and will be removed.

git branch master --set-upstream-to myupstream/master


1

快进了三年(请参阅我在那做的事情:-)),我尝试使用Git Bash拉出一个未跟踪的分支,并收到了

If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream-to=origin/<branch> develop

以下实现了我所需要的:

$ git branch --set-upstream-to=origin/develop develop Branch 'develop' set up to track remote branch 'develop' from 'origin'.


-1

使用此命令:

git clone [<options>] [--] <repo> [<dir>]

嗨,您可能想重新阅读这个问题-它提到再次克隆存储库,但是问题是关于何时首先创建本地存储库并且最初并未跟踪远程存储库。
SpoonMeiser
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.