How do you fork your own repository on GitHub?
Asked Answered
S

14

281

I have a public repository on GitHub. I want to replicate/copy it and work on a new project based on this repository, but I don't want to affect how it is now. I tried forking it using the GitHub UI but it didn't do anything.

Subdebutante answered 9/6, 2012 at 19:22 Comment(10)
What exactly are you trying to achieve? Maybe you can solve it by just branching?Cockaigne
I want to get a copy of the project and extend that project to do something else but without affecting original project. That was it. Any ways, its solved.Subdebutante
@Cockaigne .. Sorry, I thought the solution by mcepl solved my need but, it didnt. After I clone the project using 'git clone', I don't want to point to the original repo but to a new repo on github that i created. How can I achieve that?Subdebutante
@Cockaigne Did you get my questions?Subdebutante
#9743177Slit
possible duplicate of How can I fork my own GitHub repository?Ilka
The benefit of fork compared to creating a new repo is that the graphs and network history are linked together. It's a pity that Github don't allow this.Karolekarolina
@Cockaigne One repo is a project template, another is a new project that I want to create from the template.Oestrin
Fork it with another account, rename that fork and transfer the ownership to your main accountAswarm
@FredFranz - has the answer to this. Please be very wary of all other answers here. This seems like a recipe for disaster. If you're not sure what the difference is between a fork and clone, you could end up months down the road and realize you can't update or split your code with a clone. Please be sure you don't NEED a fork before attempting any of the other solutions here.Celiotomy
N
316

I don't think you can fork your own repo.
Clone it and push it to a new repo is good but you need to:

git clone https://github.com/userName/Repo New_Repo
cd New_Repo
git remote set-url origin https://github.com/userName/New_Repo
git remote add upstream https://github.com/userName/Repo
git push origin master
git push --all

(see git push)

See the all process described at "Fork your own project on GitHub".


Six years later (2016), you now have the GitHub importer which allows you to import a repo from another source... including GitHub.
See "Importing a repository with GitHub Importer"

https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfoXVYeKmcpcFh0Wi2jaRA/assets/images/help/importer/import-repository.png

narf's answer (upvoted) also illustrate that process.

That will allow you to create a new repository and import the full history of the old one into the new one, using its GitHub url.

Again: what you get is a copy, not a real fork: you cannot make pull request from the new repo to the old one.

Again (bis), as stated in the comments by mpersico, this is not a TRUE FORK.

If I have a foo which is the canonical source repo for an open source project that I want other people to fork and have access to do PR, then I do not want to work in that repo, I want a fork I can use to issue proper PRs against my project.
I have solved this my creating a second account in GitHub and forking to that.

Northwester answered 10/6, 2012 at 6:27 Comment(22)
Thanks @VonC. I think you forgot to mention editing the config file.Subdebutante
@Subdebutante which config file? (beside a _netrc one, or other global config Git parameters, as detailed in https://mcmap.net/q/14105/-syncing-with-github/…) The local git config file recording the new remote origin is automatically updated by the git remote add command.Northwester
I was talking about .git/config. Unless you change the origin url, when you say "git remote add ...." , it will reject you because, the origin name already exists. But once I changed the url in the config file it works fine. The article you pointed to me shows that.Subdebutante
@Subdebutante if remote already exists, then do a git remote set-url origin https://github.com/userName/New_Repo. That will change the .git/config file for you. See git-scm.com/docs/git-remoteNorthwester
Doesn't work. I'm getting "remote: Repository not found" when I push.Exodus
@Exodus what git remote -v return?Northwester
I've deleted the directory by now but I ran git remote set-url origin <new_repo> before trying to pushExodus
@Northwester Amazing. A month later I am doing the same thing and stumble upon the same error. This is the output: MacBook-Pro:rjs nnikolo$ git remote -v origin https://github.com/username/new_repo (fetch) origin https://github.com/username/new_remo (push) upstream https://github.com/username/old_repo (fetch) upstream https://github.com/username/old_repo (push) So your solution, unfortunately, doesn't work.Exodus
@Exodus Please post this as a new question: more people will be able to help that way.Northwester
@Northwester I am sure some mod will flag it as a duplicate (happened too often to me) - so see no point of doing it. You should, however, amend your answer as it doesn't work. A fast hack to solve the problem is to set up manually the repo - I did it and now I can push. A small mystery is, however, that some local changes seem to not appear in the push.Exodus
@Exodus No trust me, no duplicate on this one: explain the context, link back to this question and benefit from Stack Overflow instead of burying an issue in the comments.Northwester
@Northwester I just noticed that I posted a solution a month ago as a post - but it is at the very bottom of this SO question so I didn't see it today. Tbh, I see myself my post as a sort of duplicate but, unfortunately, your solution didn't work for me.Exodus
I now have two remotes: origin and upstream. What gives?Valero
@aus that is expected, for you to fetch from upstream. See https://mcmap.net/q/110177/-cannot-resolve-github-conflict-in-prNorthwester
You could delete the .git directory and start new.Splashboard
I still feel that forking from repo in same organization should be allowed. What if there's a central repository that's generic to everybody and you want to get updates from it all the time and at the same time do specific changes to your forked copy based on your needs?Finella
@Finella You could make a branch (you can make pull request from a branch to another branch from within the same remote repo)Northwester
What if the author of the central repo doesnt give you write access so you can't create a branch. all you can do is fork it.Finella
@Finella In that case, all that remain is a basic clone, and send patch by emails ;) But depending on your "central organization", you can configure a repo hosting server to allow a branch creation per user, for instance with gitolite (gitolite.com/gitolite/user/#personal-branches). But for GitHub organizations, that wouldn't be a valid option, of course.Northwester
Not the answer. The OP wanted, and I want, a TRUE FORK. If I have a foo which is the canonical source repo for an open source project that I want other people to fork and have access to do PR, then I do not want to work in that repo, I want a fork I can use to issue proper PRs against my project. I have solved this my creating a second account in GitHub and forking to that.Cosy
@Cosy Agreed. I have included your comment in the answer for more visibility.Northwester
@Northwester - thank you. Sorry about the apparent anger in that "Not the answer" comment. Just annoys the &@#%@^(()@# out of me that something as simple as a self fork never occurred to anyone at GitHub. Then again, who knows what devs think; I just came across two processes today that send out notification of execution BEFORE running the process so that on a failed process, the user gets a successful notification. Ugh. <end rant>Cosy
G
196

A super easy way to do it in 30 seconds from the GitHub website:

  1. Copy your repo's URL. Ex: https://github.com/YourName/YourOldRepo (hint: it's the URL when you look at your repo's main page on github.
  2. Click the + icon in the top right corner.
    https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpKnMAWVMrKmltX1ywKmMva3/9zJgo.png
  3. Select "Import repository".
  4. Where it asks for the "Old URL", paste the URL you copied at step #1
    https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpKnMAWVMrKmltX1ywKmMva3/vgWd3.png
  5. Enter the name of your new repo and click Begin Import.
  6. That's it! You now have a copy of the full repo, with all commit history and branches!

Limitations: It's not actually a real fork. It's a copy of the repo. It won't allow to do pull requests back and forth.

Guenna answered 23/4, 2017 at 21:3 Comment(0)
I
21

Although it is not possible to fork your own repo into the same account, it can be done into an self-owned Organization account, which can be easily created for free via the '+' button.

The main advantage of this option is that the new repo is a real fork of the original one, and not just a clone. This means that you can, for example, update changes in the orginal repo into the new one (which is not the case for a cloned repo).

The only disadvantage I see is that the forked repo won't appear under the user profile but under the organization one.

Interplay answered 13/10, 2020 at 19:4 Comment(3)
This is the only answer, that actually answers the question. I can not stress enough that a copy/clone is not a fork. I highly suggest being wary of other answers on this page.Celiotomy
I tried transfering it back to my account after that with a different name, but github doesn't allow it, gitlab doesn't have this problemApocrypha
100% works, thx u !Cufic
P
16

You can now mark the base repository as a template (in Settings, mark it as a Template repository) and on the main page of the repo, click Use this template to create your second repo.

Creating a repository from a template is similar to forking a repository, but there are important differences[1]:

  • A new fork includes the entire commit history of the parent repository, while a repository created from a template starts with a single commit.[1]
  • Commits to a fork don't appear in your contributions graph, while commits to a repository created from a template do appear in your contribution graph.[1]
  • A fork can be a temporary way to contribute code to an existing project, while creating a repository from a template starts a new project quickly.[1]

Github Help: creating a template repository creating a repository from a template

Pulsatory answered 19/12, 2019 at 5:48 Comment(0)
P
10
  1. git clone https://github.com/YOURREPO.git TargetRepoName
  2. cd TargetRepoName/
  3. git remote set-url origin https://github.com/TargetRepoName.git
  4. git push -u origin master
Pekan answered 19/7, 2015 at 12:47 Comment(2)
One might add that you need to create github.com/TargetRepoName.git before on GitHub, but except that, this answer was my first choice and it worked.Projectionist
I received an error failed to push some refs to {repo} because it didn't recognize a master branch. I had to fake it with git checkout -b master after step 3, then it worked.Kp
C
5

Simplest way to achieve the desired effect is to create a new repository, then select the import option and supply the URL of the repo you wish to fork.

Images below will help:

Fork own repo via import-1

Fork own repo via import-2

Cyan answered 30/11, 2016 at 17:41 Comment(0)
T
4

Just clone it, create new blank repo, and push to it.

Tion answered 9/6, 2012 at 19:37 Comment(4)
I cloned it to my local machine and created a blank repo but, when I try to git origin github.com/userName/New_Repo.git .. it says remote origin already exists. How should I solve that?Subdebutante
And now when I push it is changing the original repo.Subdebutante
Can you please tell me how to push to the new repo? I did the clone and creating new repo but i couldnt push to the new repo.Subdebutante
You have to change the remotes. Otherwise, it's going to try to push to the same location. git remote rm origin followed by git remote add origin URL-to-new-repositoryBudget
P
1

I followed these official instructions for "Duplicating a repository" and it seemed to work.

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

To create a duplicate of a repository without forking, you need to run a special clone command against the original repository and mirror-push to the new one. This works with any git repository, not just ones hosted on GitHub.

Pasadis answered 10/11, 2014 at 8:53 Comment(0)
E
1

The accepted solution of VonC, unfortunately, did not work for me as I got

remote: Repository not found

What did work 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.

Exodus answered 24/7, 2015 at 21:48 Comment(0)
B
1

For non tech savvy using GitHub, here is one simple solution as an alternative to other great answers above. What you need is just a GitHub Desktop application.

  1. Open your own project repo from browser, and download as a zip, eg your-project-master.zip.
  2. Unzip and rename it as your new repo.
  3. Open GitHub Desktop, and add your new repo by browsing it to your unzipped local path new repo. enter image description here
  4. Publish it to your github, by clicking the publish button. Don't forget to add the name and the description :)
Bargello answered 28/12, 2016 at 3:48 Comment(0)
C
1

STEP BY STEP

First you should create an access token

enter image description here

enter image description here

enter image description here

enter image description here

Then:

Import your existing repository

enter image description here

Here:

Enter a name for your new repository

enter image description here

Now the important step

In the password block, you have to enter the access token that you generated

enter image description here

Chole answered 29/1, 2023 at 12:26 Comment(1)
Sure, on that way you get a linked history, but I think you wont have the github fork options (compare and Pull-requests between both repos)Cranium
W
0

Just tried this, and it worked:

  1. Fork your repo into an organization account
  2. Rename it
  3. Transfer ownership back to yourself
Wainwright answered 9/2, 2014 at 17:15 Comment(2)
Nice idea but I just tried this and got an error: aspiers already has a repository in the UPSTREAM/NAME network.Nectarous
this is because they now have redirects after you rename or transfer your repos github.com/blog/1508-repository-redirects-are-hereExosmosis
S
-1

When you create a new repo, you can import from another repo with the repo .git url. It took me 30 seconde.

Scotopia answered 7/4, 2017 at 12:41 Comment(0)
S
-2
  1. create a new empty repo on github webfrontend
  2. in a clone of the older repo, replace the remote and push:
git remote remove origin
git remote add origin [email protected]:account/new.git
git branch -M main
git push -u origin main

This combines the answer of @mcepl with the comment of @wadesworld and is very similar to the official solution shown when creating a fresh repository on github (under the headline "…or push an existing repository from the command line")

Standley answered 28/5, 2021 at 14:58 Comment(1)
This is downvoted, because what you have achieved here is a normal 'clone' and not a 'fork'. Forks have specific properties that make them useful for specific situations. They are not the same thing.Celiotomy

© 2022 - 2024 — McMap. All rights reserved.