105

I have a repo on github that contains a web application that's deployed to two different domains. The application has slight logic forks here and there to behave differently depending on which domain it's deployed to.

It's come to the point to where I want to split it into two separate repos, one for each domain.

Github won't let me fork it into the same organization. Searching for "git duplicate repo" suggests I should bare clone and mirror push it, but that seems to be for keeping both repos in sync, which I don't want to do.

What's the best way to go about this? I'd like to preserve the old commit history in the new copy if possible.

Sebastian
  • 2,896
  • 23
  • 36
jemminger
  • 5,133
  • 4
  • 26
  • 47
  • 2
    Actually, the `git clone --bare` and `git push --mirror` options are exactly what you want. This does not keep both repos in sync. But it does *preserve everything*, including all branches, tags, etc. Just do this in a temporary directory to make a copy. Then clone the new copy directly from your remote in your project directory. – vastlysuperiorman Sep 24 '19 at 22:09

8 Answers8

119

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.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • 3
    Ok, and these won't track each other, right? Meaning they become two separate disconnected repos? That's what I want. – jemminger Mar 31 '14 at 17:14
  • 3
    Neither repository will update unless you `push` to it explicitly, and the "new" repository will not display any relation to the old one. – larsks Mar 31 '14 at 17:16
  • 3
    You could also push all the branches: `git push --all origin` – Marius Apr 06 '15 at 07:52
  • This worked for me, but I had to change to a different format for the URL: $ git remote set-url https://github.com/me/myrepo-new.git – Enfors May 04 '18 at 07:23
  • Might be worth also noting, if you already have your original repo on your comp and then just run ``git remote set-url origin git@github.com:me/myrepo-new`` before you push to the new repo you are probably going to want to run ``git remote set-url origin git@github.com:me/origina-repo`` afterwards to set your remote back to the original one. – hifilorau May 16 '22 at 13:24
  • I understand that the question in the body is different from the question in the title, but this is not a valid answer for the question in the title. Forking includes many more functionalities than simply "copying" a repository, such as pulling and pushing from the parent repository, which the solution to this answer does not support. – Eduardo Pignatelli Oct 28 '22 at 16:15
22

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 used to be able to read that article here--dead link.

2023 update: GitHub now supports forking a repo into the same organization. See Improved innersource collaboration and enterprise fork policies

joanis
  • 10,635
  • 14
  • 30
  • 40
Nevik Rehnel
  • 49,633
  • 6
  • 60
  • 50
  • 7
    Contacted support, their response: "At this time, an account cannot own two repos in the same network." They suggest setting up a [mirror repo](https://help.github.com/articles/duplicating-a-repository/) instead. – Steve Rice Oct 07 '14 at 23:45
  • 1
    ahw that sucks. sorry, I did not know they had changed their policy :( – Nevik Rehnel Oct 08 '14 at 06:32
  • Unfortunately, that article link is stale (I'd suggest an edit but the edit queue is full at this time) – SRNissen May 27 '23 at 08:35
  • Note that forking into the same organization only seems to be available with Github Enterprise Server, not github.com. See @VonC answer below. – David_G Jun 19 '23 at 23:54
17

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.

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
sarob
  • 187
  • 1
  • 2
  • How were you unable to make this option work?I just retested it and it works in two easy steps. Alternatively, create a local repo in github, then use the import feature there, e.g. https://github.com/sarob/mytest/import . – sarob Sep 23 '19 at 20:44
  • Could be something with our GHE vs github.com but it doesn't work over there. – serraosays Oct 14 '19 at 04:17
  • great solution! – maxpill Mar 26 '21 at 21:57
9

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.

Jamie Mason
  • 116
  • 1
  • 2
  • This is a great modern alternative since github will mark the new repo as "generated from ". There's the caveat that you will lose your git history, but if it's a hard fork that shouldn't matter as much. You can also unmark the original repo as a template once you are done. – Dylan Madisetti Jun 01 '23 at 15:15
6

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
Vinay Vemula
  • 3,855
  • 1
  • 21
  • 24
2

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.

csaboable
  • 163
  • 8
1

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.

originate
  • 21
  • 1
1

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250