Fork Your Own Repo on GitHub

A common timesaving 'hack' of mine when building 'new-but-related-to-old' projects is to fork an existing code base and use it as the boot-strap for the new project. This comes in really handy when building out multiple similar tools like my parody twitter bot army. GitHub doesn't let you fork your own repository into a new repo, so here's how I get around a fairly obvious missing feature.

All of the following commands have been tested out on macOS running the zsh system shell, but the commands should be portable to both Linux and the bash command-line provided with most Windows git distributions.

Create the new project's repository

Using GitHub's web interface, create a repository to store your new project.

Create a local working directory

GitHub doesn't allow you to clone your own repository using their web interface, so you'll have to create a local directory to pipe all of your commits through.

mkdir new-project-dir  
cd new-project-dir  
git init .  

You should keep this directory around as not only is it your local copy of your new project but if you want to bring through any new commits from your old project this will be the only place they're linked to. GitHub will not know you've forked this project from your old one, so their web interface will not offer any of its nice pull request UIs to update code across projects.

Link the repositories

Now that we have a local repository set up, we need to link in the old and new remote project repositories. I like to use upstream as the name for the old-project and origin for the new-project repository.

git remote add upstream https://github.com/user-name/old-project.git  
git remote add origin https://github.com/user-name/new-project.git  

I like using origin for the name of the new project as this matches what I'm used to with normal, single remote, repositories. I find upstream a useful name for the old repository as this matches the naming conventions used in familiar open-source projects such as Debian and Ubuntu.

Pull and push the commits

To copy the commit history from the old project and apply it to the new project we first need to pull from our old-project. This gives us a copy of the commits in the local directory. We then need to push these commits to the new-project repository, sending them to Github's servers.

git pull upstream master  
git push origin master  

At this point, the new-project repository on GitHub's servers now matches the old-project's contents and commits. As long as you keep your local copy with both remotes linked, you can continue to pull updates and diffs from the old project and apply them to the new project.