GitHub:如何将公共存储库的分支私有化?

zer*_*lus 228 github

如何分叉公共存储库,但我的fork是私有的?我确实有订阅支持私有存储库.

Mar*_*cek 363

答案是正确的,但没有提到如何在公共repo和fork之间同步代码.

这是完整的工作流程(我们在开源React Native之前完成了这个工作):


首先,像其他人所说的那样复制回购(详情请点这里):

private-repo通过Github UI创建一个新的repo(让我们称之为).然后:

git clone --bare https://github.com/exampleuser/public-repo.git
cd public-repo.git
git push --mirror https://github.com/yourname/private-repo.git
cd ..
rm -rf public-repo.git
Run Code Online (Sandbox Code Playgroud)

克隆私人仓库,以便您可以处理它:

git clone https://github.com/yourname/private-repo.git
cd private-repo
make some changes
git commit
git push origin master
Run Code Online (Sandbox Code Playgroud)

为了从公共回购中获得新的热情:

cd private-repo
git remote add public https://github.com/exampleuser/public-repo.git
git pull public master # Creates a merge commit
git push origin master
Run Code Online (Sandbox Code Playgroud)

很棒,您的私人仓库现在拥有来自公共仓库的最新代码以及您的更改.


最后,创建一个拉请求私有仓库 - >公共仓库:

创建拉取请求的唯一方法是对公共仓库进行推送访问.这是因为你需要推到那里的分支(这就是原因).

git clone https://github.com/exampleuser/public-repo.git
cd public-repo
git remote add private_repo_yourname https://github.com/yourname/private-repo.git
git checkout -b pull_request_yourname
git pull private_repo_yourname master
git push origin pull_request_yourname
Run Code Online (Sandbox Code Playgroud)

现在只需创建公共回购通过Github上UI拉请求,如描述在这里.

一旦项目所有者审核了您的拉取请求,他们就可以合并它.

当然可以重复整个过程(只需省略添加遥控器的步骤).

  • 为什么不能简单地将空的私有仓库作为新的远程仓库添加到公共仓库的正常克隆中,然后推送到远程仓库?会有什么不同? (5认同)
  • @FalkoMenge 因为走镜像路由会复制所有分支、标签等。在最简单的情况下,如果存储库只有 1 个分支,没有标签,没有其他特别的东西,那么我认为你的方法会起作用。在所有其他情况下,最好进行镜像。 (4认同)
  • @FalkoMenge 我有同样的问题。阅读 https://www.git-scm.com/docs/git-clone,听起来像一个普通的 `git clone` 会设置远程跟踪分支,也许你并不真正想要克隆的 repo 中的一些其他配置. 而 --bare clone 只是按原样从远程复制 .git 目录。 (2认同)

szy*_*dan 91

现在还有一个选择(2015年1月)

  1. 创建一个新的私人仓库
  2. 在空的回购屏幕上有一个"导入"选项/按钮 在此输入图像描述
  3. 点击它并放入现有的github repo url没有提到github选项,但它也适用于github repos. 在此输入图像描述
  4. DONE

  • 这虽然有点不同,但仍有效.(我在2015年8月尝试过).我没有找到导入选项/按钮,因此用谷歌搜索它并最终在此URL中.https://import.github.com/new在这里,您可以输入现有的github网址,然后点击*检查*按钮.验证后,您可以输入新仓库的名称,然后单击"私人"按钮,然后单击"*开始导入"* (19认同)
  • 在撰写本文时,URL 似乎应该是 https://github.com/new/import。 (4认同)
  • 我相信@MattvanAndel意味着评论是正确的,而不是原来的答案.我也关注了湿婆的评论并且有效.请注意,您不应该首先创建本地私有存储库! (2认同)
  • @Shiva如果你从你的评论中做出回答,我会赞成的. (2认同)
  • 不支持:根据 GitHub 支持,导入仅用于将外部项目转换为 github,在 github 存储库上使用它可能会产生意想不到的后果。显然,基于此处的答案,这适用于某些项目,但我在大型项目上使用 import 时遇到了问题(这就是我寻求 github 支持的原因)。在我看来,当复制仓库很简单时,不值得冒这个风险。 (2认同)

ste*_*ano 35

目前的答案有点过时,为清楚起见:

简短的回答是:

  1. 做一个公共回购的克隆.
  2. 创建一个新的私人.
  3. 做一个镜子推到新的私人.

这在GitHub上有记录:duplicating-a-repository

  • 有没有办法拉动上游变化?这对于拥有自由许可证的回购非常重要 - 我已将我的回购私有化,但仍希望合并上游变更. (3认同)
  • 是.您应该能够将其他repo添加为新的(跟踪)远程(例如'other-repo'),然后定期从中获取和合并更改(例如'git merge other-repo/stable'). (2认同)

bsu*_*tor 25

你必须复制回购

你可以看到这个doc(来自github)

要在不分叉的情况下创建存储库的副本,您需要针对原始存储库运行特殊克隆命令并镜像推送到新存储库.

在下列情况下,您尝试推送的存储库(例如exampleuser/new-repository或exampleuser/mirrored)应该已存在于GitHub上.有关更多信息,请参阅"创建新存储库".

镜像存储库

要完全复制,您需要执行裸克隆和镜像推送.

打开命令行,然后键入以下命令:

$ git clone --bare https://github.com/exampleuser/old-repository.git
# Make a bare clone of the repository

$ cd old-repository.git
$ git push --mirror https://github.com/exampleuser/new-repository.git
# Mirror-push to the new repository

$ cd ..
$ rm -rf old-repository.git
# Remove our temporary local repository
Run Code Online (Sandbox Code Playgroud)

如果要在其他位置镜像存储库(包括从原始位置获取更新),则可以克隆镜像并定期推送更改.

$ git clone --mirror https://github.com/exampleuser/repository-to-mirror.git
# Make a bare mirrored clone of the repository

$ cd repository-to-mirror.git
$ git remote set-url --push origin https://github.com/exampleuser/mirrored
# Set the push location to your mirror
Run Code Online (Sandbox Code Playgroud)

与裸克隆一样,镜像克隆包括所有远程分支和标记,但每次获取时都会覆盖所有本地引用,因此它将始终与原始存储库相同.设置推送的URL简化了推送到镜像的过程.要更新镜像,请获取更新和推送,这可以通过运行cron作业自动完成.

$ git fetch -p origin
$ git push --mirror
Run Code Online (Sandbox Code Playgroud)

https://help.github.com/articles/duplicating-a-repository


DJT*_*ano 14

只需转到https://github.com/new/import

GitHub 导入

在“您的旧存储库的克隆 URL”部分中粘贴您想要的存储库 URL,然后在“隐私”中选择Private

  • @cb4好吧,如果你不说你遇到了什么问题,基本上它并没有增加价值。 (4认同)
  • 使用这个,我将像往常一样收到主分支中的更改通知,然后将它们合并到我的代码中? (2认同)

Yid*_*dir 11

GitHub现在有一个导入选项,让您可以选择想要新导入的公共存储库还是私有存储库的任何内容

Github仓库导入


cb4*_*cb4 7

2021 年更新

\n

我是 git 新手,所以想在 eclipse GUI 中尽可能多地进行操作(v2020-12;EGit v5.11)。详细信息如下。

\n

--->其他答案中的导入方法仅为 Subversion、Mercurial 或 TFS 项目设计。据我亲身了解, GitHub 不支持 git 项目。它可能有效,但为什么要冒险呢?

\n
\n

存储库和 GitHub 连接

\n

eclipse/org.aspectj原始公共仓库upstream用于获取的远程
\ncb4/org.aspectjfork,用于origin推送的远程
\ncb4/remPrivAJ远程私有仓库,用于private推送的远程
\nI:\\local本地仓库

\n

对于 github 身份验证,我使用 ssh 和 ed25519 密钥(此 SO 答案中的步骤),因此我的连接 URI 如下所示ssh://git@github.com/<user>/<repo>

\n

表示法: -> 是鼠标点击或者选择;right-> 是右键单击。

\n
\n

脚步

\n
    \n
  1. 在 github 上,fork将原始公共存储库
  2. \n
  3. 在 github 上,创建空的远程私有存储库
  4. \n
  5. 在 eclipse Git Perspective 中,将 fork 克隆到新的本地存储库并进行配置
  6. \n
\n
    \n
  • 3.1 -> Clone a Git Repository and add clone to this view -> Clone URI -> Next\n
      \n
    • 3.1.1 根据需要输入 URI、连接和身份验证信息-> Next
    • \n
    • 3.1.2. 选择所需的分支(我只选择了master)-> Next
    • \n
    • 3.1.3. 输入本地存储库的目标目录
    • \n
    • 3.1.4. 现在导入项目或稍后导入,然后-> Finish填充本地存储库
    • \n
    \n
  • \n
  • 3.2. 将原始公共存储库配置为新的远程存储库,命名upstream为拉取更新\n
      \n
    • 3.2.1. right-> Remotes -> Create Remote ->并输入姓名upstream
    • \n
    • 3.2.2. -> Configure fetch -> Create -> Change ->输入原始存储库 URI-> Finish
    • \n
    • 3.2.3. -> Save and Fetch ->所有分支均已下载-> Close
    • \n
    • 3.2.4. 注意:我只想要主分支,所以这样做而不是步骤 3.2.3
    • \n
    • 3.2.5. -> Advanced在 下Specifications to fetch,删除那里的单独条目
    • \n
    • 3.2.6. 在下面Add create/update specification -> Source ref: dropdown并选择master [branch] -> +Add Spec -> Force Update
    • \n
    • 3.2.7. -> Save specifications in upstream configuration -> Finish -> Save and Fetch ->仅下载主分支-> Close
    • \n
    \n
  • \n
\n
\n
    \n
  1. 将远程公共存储库复制到远程私有存储库并进行配置。这是\xe2\x80\x99无法在eclipse中完成的唯一步骤,因此我安装了Git for Windows并使用Gi​​tCMD。
  2. \n
\n

就像这样:

\n
git clone --bare git://github.com/eclipse/org.aspectj.git tmpRepo\n<if prompted, enter ssh passphrase>\ncd tmpRepo    \ngit push --mirror ssh://git@github.com:cb4/private.git\n<if prompted, enter ssh passphrase>\ncd ..    \nrmdir tmpRepo /s /q\n
Run Code Online (Sandbox Code Playgroud)\n
\n
    \n
  1. 将远程私有存储库配置为名为private推送的新远程
  2. \n
\n
    \n
  • 5.1. right-> Remotes -> Create Remote ->并输入姓名private
  • \n
  • 5.2. -> Configure push -> Create -> Change ->输入远程私有存储库 URI-> Finish
  • \n
  • 5.3.-> Advanced -> Add All Branches Spec -> Force Update -> Save specifications in origin configuration -> Finish
  • \n
  • 5.4. -> Save and Pushmaster 分支被推送-> Close
  • \n
\n

此时,您已将公共存储库分叉到您的私有存储库中。要了解如何将私有更改推送到您的分叉,然后针对原始公共存储库打开拉取请求,请参阅我的答案此处。生成的配置如下所示:
\n配置

\n