Forking one of your own Github repositories ought to be easy, right? After all, forking somebody else’s repo is as simple as clicking a single button! Surely you can just press that same button on your own repo?
NOPE!
If you press the Fork button on your own repo, the page will refresh and… that’s it. No error message, no suggested course of action, nothing. It turns out forking your own repo on Github is impossible, but don’t worry: following the steps below will get you the next best thing.
1. Create a New Repo On Github
First, go to github.com and create a new repository. This will contain our fork when we’re done. I’ll refer to this repo as “fork-repo”, with the original being “orig-repo”.
Make sure you don’t check the box for “Initialize this repository with a README”. You’ll see why in Step 4!
2. Clone the New Repo Locally
Next, make a local copy of the blank repo we just made. In Terminal, cd to the base directory you want to keep the fork in and then type the following:
git clone https://github.com/YourUsernameHere/fork-repo.git
3. Add an Upstream Remote
We’ll now add an upstream remote pointing at the original repo. This will allow us to pull files from the original repo, both now and in the future if we wish. Make sure you navigate to the directory you cloned the fork repo into first!
cd fork-repo git remote add upstream https://github.com/YourUsernameHere/orig-repo.git
4. Pull From the Original Repo
Now we can pull all the files from our original repo into the fork, like so:
git pull upstream master
Your fork directory should now be identical to your original repo!
Note that if you made a README.md for the new repo (or added any other file) you may have some merge conflicts to resolve before you can go to the next step. Make the necessary changes to your files and commit to resolve the conflict.
5. Push!
You’re done! Well, locally at least. All that’s left is to push your new fork repo back up to Github:
git push origin master
Thank you! This was just what I needed :)
I actually spent time on GitHub looking for a button to push, that would do all the above steps for me. So it’s a bit mad that one must utter these incantations, but thank you for saving us all the time with your post. Cheers – Peter
Very helpful! Than you!
Super helpful and not immediately obvious to me as the message when you try and fork your own repo says “You already have a fork of this repository” (by contrast NOT super helpful!)
Thank you.
Super tutorial, thank you! One question though, how to pull the changes into this “branch” from the original repo at a later date?
Thank you this was helpful but there is a missing step which is not obvious for a beginner (as I found myself).
After step 2:
git clone https://github.com/YourUsernameHere/fork-repo.git
You must initialise the repository with:
git init
Otherwise, if you go on to step 3 (git remote add upstream https://github.com/YourUsernameHere/orig-repo.git) without this, it throws the following error:
Not a git repository (or any of the pa rent directories): .git
Hmm that’s not correct it worked for me without `git init`. Maybe you missed the `cd fork-repo` at the start of Step 3?
THANK YOU !
Thank you. It works.
You rock!