将 github 上的 git repo 复制/分叉到同一个组织中

我在 github 上有一个回购,它包含一个部署到两个不同域的 Web 应用程序。根据部署到的域的不同,应用程序在这里或那里有一些不同的逻辑分支。

现在我想把它分成两个独立的回购协议,每个域名一个。

Github 不让我把钱分给同一个组织。搜索“ git 副本回购”建议我应该裸克隆和镜像推它,但这似乎是为了保持两个回购同步,我不想这样做。

最好的办法是什么?如果可能的话,我希望在新的副本中保存旧的提交历史。

41437 次浏览

Just create a new repository and push to it from your working copy:

git clone git@github.com:me/myrepo-original
cd myrepo-original
git remote set-url origin git@github.com:me/myrepo-new
git push origin master

Now you have a new repository, myrepo-new, which is identical to myrepo-original.

If you do not need the fork relation (e.g. you want some kind of decoupled alternate repo for whatever reason), duplicating the repo as outlines by your Google finds and larsks's answer is fine.

If you do want to make it a fork, contact Github support (support@github.com or https://github.com/support), and they will create a fork in the same organization for you. (They're not picky about this either, you'll have just to provide an alternative name for the repo, as repo names within an account must be unique.)


Update: User Steve Rice reports in the comments below that GitHub Support stated that support would not currently/no longer set up a second fork in your account.

GitHub recently posted an article about possible alternatives to forking a repo into the same account. You can read that article here.

Another way would be to add the original repo, to be copied, as remote for our current repo.

#create a new repo in the org1 organization called myrepo-new

In your local terminal, run:

git clone git@github.com:org1/myrepo-new
cd myrepo-new
git remote -v #shows current repo link on github as origin
git remote add name-for-remote https://github.com/org1/repo-old
git remote -v #shows repo-old as name-for-remote
git fetch  name-for-remote
git merge name-for-remote/branch-to-get-from-remote
#Now fix any conflicts if present
#If number of files/commits is very high, the desktop client may hang when you try to commit your changes after merge. Try switching to Git Shell if this happens.
git status
git commit -m "commit message"
git push origin master

Use Github's Import Repository option on the + menu on top of the page or just open

https://github.com/new/import

This creates a new repository with the exact contents of the copied repository. The downside (the same as for git commands answer by Iarsks) is that it doesn't count as a fork for Github.

Alternative solution:

Create a new organisation and fork it there. If the repo is private, it will stay private in the new org as well. Then you can give access to external devs or whoever you want to the forked repo and they can raise PRs back to original repo.

I know this is a super old thread, but I stumbled across it looking for the the answer and subsequently found a better way:

Make the repo you want to copy a template repo. It is a check box at the top of the repository settings. After you do this, there will be a big green button labeled "Use this template" at the top of the repo next to the Code button. Click that, and it will make a copy of the repo in whichever account you want.

You can mark the first repo as a Template (in settings) and then "Use" that template repo when you create a new repo. It's not exactly a fork, but functionally equivalent in some respects.

Github won't let me fork it into the same organization

GitHub does let you fork within the same organisation, since June 2022
(But only for GHES -- GitHub Enterprise Server --/GH Cloud, not github.com).

Improved innersource collaboration and enterprise fork policies

Previously, three aspects of repository forks caused friction to innersource collaboration and administration:

  1. Repositories could not be forked within a single organization.
  2. Repositories with internal visibility could not be forked to an organization.
  3. Enterprise owners lacked control over where repositories could be forked.

These obstacles have been addressed with the following new features. We're always looking for new ways to improve repository collaboration and we welcome your ideas.

Fork a repository to the same organization as its parent

Previously, a repository could be forked only to a different organization or user account.

Now, a repository can be forked to the same organization as its parent repository, addressing situations where people are working in one organization and don't want to fork a repository to a different organization or user account.