19

I'd like to create a bower package / stylesheet for League of Moveable Type's Chunk typeface amongst other similar tasks.

I'm wondering if it's possible to fork their "webfonts" directory into a "fonts" directory in a new repo. This would allow me to create a bower.json file and stylesheet.

Thanks,

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
christian
  • 2,279
  • 4
  • 31
  • 42
  • [splitting-a-subfolder-out-into-a-new-repository](https://docs.github.com/en/get-started/using-git/splitting-a-subfolder-out-into-a-new-repository) – T.Todua Jan 02 '22 at 19:58

2 Answers2

24

I don't think you can directly fork it on the web UI on github, but if you are ok cloning it and pushing things manually, you can do the following

  • Clone the repo

    git clone https://github.com/theleagueof/chunk
    
  • Create a branch using the git subtree command for the folder only

    git subtree split --prefix=folder_name -b new_branch
    
  • Create a new github repo
  • Add this new repo as a remote,

    git remote add upstream https://github.com/user/repo
    
  • Push the subtree

    git push upstream new_branch
    
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • 4
    Any idea how to make the original repo recognise a "fork" created this way as a legitimate fork and allow submitting PRs ? – vucalur Jun 15 '15 at 15:13
  • @vucalur hmm no, direct pull requests won't work. What you _can_ try to do is have the original repo as a remote in this new forked repo, push to the original repo, perform a subtree merge (reverse of split) on a fresh branch, and now issue a PR from this new branch. – Anshul Goyal Jun 15 '15 at 16:06
  • 4
    To push the `new_branch` to the `master` branch of the new github repo, use `git push upstream new_branch:master` – bastelflp Feb 05 '16 at 11:31
  • 1
    the branched repo can be updated by rerunning the `subtree split` command with `--onto new_branch -b new_branch` (per https://stackoverflow.com/questions/24577084/forking-a-sub-directory-of-a-repository-on-github-and-making-it-part-of-my-own-r) – Nora Powers Mar 06 '20 at 05:21
  • But this is just copying files and their history into a new repository, you loose the "fork" link on github and in the UI you cannot see changes against the fork ... – Got To Figure Apr 30 '23 at 09:57
12

Github has posted an article how to do this at https://help.github.com/en/articles/splitting-a-subfolder-out-into-a-new-repository:

You can turn a folder within a Git repository into a brand new repository.

If you create a new clone of the repository, you won't lose any of your Git history or changes when you split a folder into a separate repository.

  1. Open Terminal.

  2. Change the current working directory to the location where you want to create your new repository.

  3. Clone the repository that contains the subfolder.

    $ git clone https://github.com/USERNAME/REPOSITORY-NAME
    
  4. Change the current working directory to your cloned repository.
    $ cd REPOSITORY-NAME
    
  5. To filter out the subfolder from the rest of the files in the repository, run git filter-branch, supplying this information:

    • FOLDER-NAME: The folder within your project that you'd like to create a separate repository from.

    • BRANCH-NAME: The default branch for your current project, for example, master or gh-pages.

    $ git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME  BRANCH-NAME 
     # Filter the specified branch in your directory and remove empty commits
     > Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (89/89)
     > Ref 'refs/heads/BRANCH-NAME' was rewritten 
    

    The repository should now only contain the files that were in your subfolder.

  6. Create a new repository on GitHub.

  7. At the top of your new GitHub repository's Quick Setup page, click the clipboard to copy the remote repository URL.

    • Copy remote repository URL field
  8. Check the existing remote name for your repository. For example, origin or upstream are two common choices.

    $ git remote -v
     > origin  https://github.com/USERNAME/REPOSITORY-NAME.git (fetch) 
     > origin  https://github.com/USERNAME/REPOSITORY-NAME.git (push)
    
  9. Set up a new remote URL for your new repository using the existing remote name and the remote repository URL you copied in step 7.
    git remote set-url origin
    https://github.com/USERNAME/NEW-REPOSITORY-NAME.git
    
  10. Verify that the remote URL has changed with your new repository name.
    $ git remote -v
     # Verify new remote URL
     > origin  https://github.com/USERNAME/NEW-REPOSITORY-NAME.git (fetch)
     > origin  https://github.com/USERNAME/NEW-REPOSITORY-NAME.git (push)
    
  11. Push your changes to the new repository on GitHub.
    git push -u origin BRANCH-NAME
    
user3756236
  • 121
  • 1
  • 4
  • On large repositories, this answer seems to perform orders of magnitude faster – Nora Powers Mar 06 '20 at 05:07
  • @NoraPowers compared to what? – Csaba Toth Sep 02 '20 at 21:31
  • The new repo I created had a README, that prevented me to push, because I needed to pull, but that errored out with `fatal: refusing to merge unrelated histories` – Csaba Toth Sep 02 '20 at 21:48
  • Plus after I performed the `git filter-branch --prune-empty` the repo did not contain the subfolder only. Still all the other folders were there. So this step-by-step seems to be crappy for me. – Csaba Toth Sep 02 '20 at 21:49
  • I repeated the steps from a clean slate. I purged the previous new repository, recreated it. This time the `filter-branch --prune-empty` worked. I have no idea why it didn't work before (maybe the other repo was clean but it had extra files (excluded by gitignore BTW) when I tested stuff with an IDE. So now I had the subfolder only, and I could push into the virgin (no README, no gitignore, no LICENSE) repository. – Csaba Toth Sep 02 '20 at 21:57
  • After doing all this the GitHub UI does not recognize that this is an actual fork. – Csaba Toth Sep 02 '20 at 22:12