22

My github repo is called Programming-iOS-4-Book-Examples, because it's the example code from my book "Programming iOS 4". Now I've written a new edition of the book, retitled "Programming iOS 5". I need to leave the old repo in place with the old name, because there are links to it all over the net and someone might need the old code. But now I also want a new repo with a new name, Programming-iOS-5-Book-Examples, containing the same examples rewritten for iOS 5 (plus some new ones).

Naturally, I saw this: How can I fork my own GitHub repository? But the advice there is to use branches. That isn't going to work for me. I don't want to use a branch because that defeats the purpose of giving the repo a name that I can link to. I want the public to find my iOS 4 examples in the iOS 4 repo and my iOS 5 examples in the iOS 5 repo.

This seems like a perfect use of a fork, but when I press the Fork button nothing happens; I'm apparently not allowed to fork my own repo.

Of course I could just make this a whole new repo, but that would mean uploading all the resources separately, which is unfortunate because everything is already right there in the iOS 4 repo. Do I just have to do that anyway?

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 2
    Do you need to plan ahead for iOS 6 and 7 and ...? If so, would you be better server by a single repository for iOS 5 and later, with a branch for each major version? Clearly, you can't change the past easily, but you can go ahead and make the next versions easier to manage. – Jonathan Leffler Mar 11 '12 at 19:52
  • 1
    +1 Puts me on a whole new train of thought. – matt Mar 11 '12 at 19:58

4 Answers4

20

You can't have two repositories with the same name, and forking on Github automatically transfers the name, so that's what keeps that from working. It sounds like you would be well served by adding a branch locally, then pushing to a new Github repository with the new name. You can even keep the Github repo showing master as the branch:

git clone git://github.com/you/repo.git
git checkout -b new_book
[ create new repo on Github ]
git remote add new_origin git://github.com/you/repo.git
git push new_origin new_book:master

Just use more appropriate names and you're golden. You can merge updates to shared examples, add additional examples to the new book code, and you just push to both origin and new_origin (using the example names above) when you make changes.

coreyward
  • 77,547
  • 20
  • 137
  • 166
  • Thx for the excellent explanation and detailed instructions. That lets me keep the same repo on my machine which is nice, but it sounds like I still have to push a duplicate of all the resources from my machine to the new github repo, which is a pity (but perhaps just unavoidable)... – matt Mar 11 '12 at 19:45
  • Github pretty much does the same thing with a fork on their end, and you still have a separate remote, so it's just about the same thing. You just don't get pull requests and networking on Github, but it sounds like that wouldn't be useful for you anyways. :) – coreyward Mar 11 '12 at 20:03
  • Thanks -- this explanation was just what I needed. I had gone through the same thought process as the OP. – ouonomos Nov 17 '15 at 18:58
9

I know this is old but I faced the same issue recently. What did work for me was the following:

  1. Create a new_repo at github
  2. git clone new_repo
  3. cd new_repo
  4. git remote add upstream old_repo.git
  5. git pull upstream master
  6. git push origin master

I got all the above from here.

Nick
  • 2,924
  • 4
  • 36
  • 43
  • 1
    [Since GIT 2.9 you'll need to use `git pull upstream master --allow-unrelated-histories`.](https://stackoverflow.com/a/37938036/7470402) I wanted to suggest an edit, but the suggested edit queue is full, so I've put this in a comment. – Tess E. Duursma Dec 03 '21 at 15:09
3

Given that you can have URL that link branches in a repo, you can still use branches, which INMHO are the natural and easy way for the case .

For example, you can have the following URL for iOS4 example: https://github.com/mattneub/Programming-iOS-Book-Examples/tree/ios4

And https://github.com/mattneub/Programming-iOS-Book-Examples/tree/ios5 for the iOS5 examples, where ios4 and ios5 are your branch names.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 1
    misread, apologies - but the fact is that you've missed the point: I can't change the name now, as old links will break – matt Mar 11 '12 at 19:55
  • thanks; totally agree, breaking old links is show-stopper, but maybe you can keep the `master` branch in sync with `ios4`, and keep the repo name to ios4? even if I agree it can sound weird, but it's just a URL... – CharlesB Mar 11 '12 at 20:12
  • You've got me thinking that maybe I *should* just change the name and break the old links. That way I don't lose my followers, if you see what I mean. – matt Mar 12 '12 at 02:48
  • 2
    If you change the name you can still leave a repo at the ancient one with just a `README.md` indicating the new repo address – CharlesB Mar 13 '12 at 00:06
2

In the end here's what I did:

  1. I renamed the existing repo. This works great (thanks, github, for making that so easy). Don't forget to edit your own git repo's config file to keep the remote branch relationship between your own master branch and the github repo's master branch.

  2. I created a new repo with the old repo's name, consisting of nothing but a README.md providing the existing repo's new URL.

Thus, I didn't end up separating the iOS 4 book content from the iOS 5 book content. Instead, I rearranged the structure of the original repo and gave it a more general name, not tied to iOS 4 in particular. And existing links to the old repo don't break, because there's a placeholder repo at that URL, pointing to the new repo.

matt
  • 515,959
  • 87
  • 875
  • 1,141