Git从另一个存储库拉出来

Lib*_*bux 82 git version-control dvcs repository

我有一个名为的存储库Generic,它是一个通用的应用程序.我已将其分叉到一个名为Acme的存储Generic库中,该存储库仅构建在应用程序存储库上,并为其添加了Acme Co品牌.

如果我对核心功能进行了更改Generic,我想Acme使用我对核心功能所做的最新更改来更新存储库Generic.我该怎么办?

据我所知,我基本上试图将上游存储库中所做的更改合并到当前的fork中.

如果它意味着什么,我正在尝试这样做,因为我有一个通用的应用程序,然后我为个人客户构建和品牌(如Acme本例所示).如果有更清洁的方法,请告诉我.

McL*_*vin 117

在您的Acme仓库中发出以下命令.它增加了一个新的名为远程仓库upstream指向Generic回购.

git remote add upstream https://location/of/generic.git
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用以下命令Generic将对所做的任何更改合并到当前分支中Acme:

git pull upstream
Run Code Online (Sandbox Code Playgroud)

如果您只是希望它在不自动合并的情况下下载更改,请使用git fetch而不是git pull.

如果要禁用推送到该存储库,请使用类似的方式将推送URL设置为无效的URL

git config remote.upstream.pushurl "NEVER GONNA GIVE YOU UP"
Run Code Online (Sandbox Code Playgroud)

Git现在会对你大吼大叫,如果你试图推进就无法找到回购upstream(对Rickroll感到抱歉,但这是第一个突然出现在我头脑中的随机字符串).

  • 我需要发出`git pull --allow-unrelated-histories upstream master` (5认同)
  • 非常好,谢谢.还有一件事:是否有一种方法可以使这个远程只读,所以我最终不会意外地推动它? (4认同)
  • 从git 2.9开始,您需要指定--allow-unrelated-histories标志.请参阅/sf/answers/2655662551/ (4认同)

小智 23

为了从不同的 repo 中提取特定分支,您可以使用以下 git 命令。

git pull <git_url> <branch> --allow-unrelated-histories
Run Code Online (Sandbox Code Playgroud)


ccp*_*zza 7

正如@vin提到的,您可以git pull直接从另一个存储库而不添加新的源(假设从另一个存储库的master分支拉到本地master分支):

git pull https://some-other-repo.git master:master

# OR pull to a new local branch
git pull https://some-other-repo.git master:new_local_branch
Run Code Online (Sandbox Code Playgroud)

如果两个存储库的提交历史记录没有链接,那么--allow-unrelated-histories将需要它,但请谨慎使用,因为这意味着您知道自己在做什么。

您还可以将git push本地存储库添加到另一个存储库,而无需将其添加为远程存储库:

git push https://some-other-repo.git master:master

# OR create a new branch on remote
git push https://some-other-repo.git master:new_remote_branch
Run Code Online (Sandbox Code Playgroud)