手动创建 Git fork

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27751578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:39:51  来源:igfitidea点击:

Manually create a Git fork

gitgithubbitbucketgit-fork

提问by metacubed

If I understand forking, it conceptually involves the following steps:

如果我理解分叉,它在概念上涉及以下步骤:

  1. Mirror-clone the source repo to a target repo
  2. Set an "upstream" remote on the target repo, pointing to the source repo
  3. Some other stuff, like email subscriptions, etc. (not important for this question)
  1. 将源仓库镜像克隆到目标仓库
  2. 在目标仓库上设置一个“上游”远程,指向源仓库
  3. 其他一些东西,例如电子邮件订阅等(对这个问题不重要)

This is how it looks like:

这是它的样子:

Original <──upstream─── Forked
(server)               (server)
                           ↑
                           │origin
                           │
                        (local)

The key difference from cloning is that these steps are server-side, not local. How do I replicate this manually, on the git command line?

与克隆的主要区别在于这些步骤是服务器端的,而不是本地的。如何在 git 命令行上手动复制它?

Here's what I've done so far:

这是我到目前为止所做的:

  1. Clone the source repo to a local repo
  2. Change the "origin" remote to point to the intended target repo
  3. Add an "upstream" remote pointing to the source repo
  1. 将源存储库克隆到本地存储库
  2. 将“源”远程更改为指向预期的目标存储库
  3. 添加指向源存储库的“上游”远程

At this stage, I have everything set up on the local repo. I can sync changes between the original and forked repos using an intermediate local clone. So this is what I have:

在这个阶段,我在本地存储库中设置了所有内容。我可以使用中间本地克隆同步原始和分叉存储库之间的更改。所以这就是我所拥有的:

Original                Forked
(server)               (server)
    ↑                      ↑
    │                      │origin
    │                      │
    └───────upstream─── (local)

Now how do I push this link to the server i.e. make the original repo an upstream remote of the server-sideforked repo, to match the first diagram?

现在如何将此链接推送到服务器,即使原始存储库成为服务器端分叉存储库的上游远程,以匹配第一个图?

Note that this question is not GitHub-specific - I might also want to do this with BitBucket. Ideally, I should be able to do this across sites as well. I've read lots of similar questions here on SO, but there's no clear answer.

请注意,这个问题不是特定于 GitHub 的 - 我可能也想用 BitBucket 来做这个。理想情况下,我也应该能够跨站点执行此操作。我在这里阅读了很多关于 SO 的类似问题,但没有明确的答案。

回答by Nikhil Supekar

You can fork a project on Bitbucket using their API on command line, but you need at least read access to source project.

您可以在命令行上使用他们的 API 在 Bitbucket 上 fork 一个项目,但您至少需要对源项目的读取访问权限。

Syntax is:

语法是:

curl -v --user {username}:"{password}" \
https://bitbucket.org/api/1.0/repositories/{accountname}/{repo_slug}/fork \
--data "name=mynewrepo"

e.g.

例如

  1. To fork a project projectABCfrom an account ABCto your account XYZwith the name ProjectXYZ, use the following command

    curl -v --user XYZ:"XYZPASSWORDXYZ" \
    https://bitbucket.org/api/1.0/repositories/ABC/ProjectABC/fork \
    --data "name=ProjectXYZ"
    

    see Bitbucket documentationfor more details.

  2. Now clone this project on your local machine,

    git clone your_target_git_repository_path
    
  3. Go to your project directory and add remote upstreamwhich will point to the source repository,

    git remote add upstream source_git_repository_path
    
  4. Now, at anytime to pull the changes from source repository (say from master branch), use:

    git pull upstream master
    

    and to push your local commits to your target repository on server, use: git push origin master

  5. And when your changes on target repository are ready to be merged with the source repository, create a Pull Request either from Bitbucket website or using Bitbucket API: Pull Request

  1. 要将项目projectABC从一个帐户分叉ABC到您的XYZ具有名称的帐户,请ProjectXYZ使用以下命令

    curl -v --user XYZ:"XYZPASSWORDXYZ" \
    https://bitbucket.org/api/1.0/repositories/ABC/ProjectABC/fork \
    --data "name=ProjectXYZ"
    

    有关更多详细信息,请参阅Bitbucket 文档

  2. 现在在你的本地机器上克隆这个项目,

    git clone your_target_git_repository_path
    
  3. 转到您的项目目录并添加upstream指向源存储库的远程,

    git remote add upstream source_git_repository_path
    
  4. 现在,随时从源存储库(例如从 master 分支)中提取更改,请使用:

    git pull upstream master
    

    并将本地提交推送到服务器上的目标存储库,请使用:git push origin master

  5. 当您对目标存储库的更改准备好与源存储库合并时,从 Bitbucket 网站或使用Bitbucket API创建一个拉取请求:拉取请求

回答by drammock

For GitHub, apparently you can now do this with the hubCLIwith the command hub fork. I haven't used it, but based on the docs it looks like you would do this:

对于 GitHub,显然您现在可以使用hubCLI命令执行此操作hub fork。我没有使用过它,但根据文档,您似乎会这样做:

git clone [email protected]:some_user_or_organization/some_project.git
cd some_project
hub fork

...and you would end up with two remotes: originpointing to the upstream, and one with the same name as your GitHub username pointing at your new fork. (Would be nice if hubalso switched the remote names to be "upstream" and "origin", respectively, but I don't think it does.)

...你最终会得到两个遥控器:origin指向上游,一个与你的 GitHub 用户名同名,指向你的新分支。(如果hub还将远程名称分别切换为“上游”和“原点”就好了,但我认为不会。)

回答by Will

When you push to your fork, try this option: --mirror

当你推送到你的叉子时,试试这个选项:--mirror

https://git-scm.com/docs/git-push#git-push---mirror

https://git-scm.com/docs/git-push#git-push---镜像

This might mean that you need to specify the --mirror option upon cloning as well to get the remotes to your local repo.

这可能意味着您还需要在克隆时指定 --mirror 选项才能将遥控器发送到您的本地存储库。

Alternatively you can include a remote setup script in your repo and reference it in the developer setup manual of your project.

或者,您可以在您的存储库中包含一个远程设置脚本,并在您项目的开发人员设置手册中引用它。

回答by Prakash Murthy

The steps to create a local copy of the repo - pointing to the final forked repo are as you you have detailed.

创建存储库的本地副本的步骤 - 指向最终的分叉存储库,如您所详述。

The next step would be to create the forked repo. It can be done from command line using Github API- not git; see https://stackoverflow.com/a/2425632/429758for more info.

下一步是创建分叉回购。它可以从命令行使用Github API-not git; 有关更多信息,请参阅https://stackoverflow.com/a/2425632/429758

After that, it is a straightforward git push origin masterto complete the manual forking.

之后,就可以直接git push origin master完成手动分叉了。

Note: Bitbucket too provides a REST API which allows for creating the forked repo from command line.

注意:Bitbucket 也提供了一个 REST API,它允许从命令行创建分叉的 repo。