Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fork subdirectory of repo as a different repo in github

Tags:

git

github

bower

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,

like image 555
christian Avatar asked Mar 27 '15 16:03

christian


People also ask

How do I fork a repo to another repo?

You can fork any repo by clicking the fork button in the upper right hand corner of a repo page. Click on the Fork button to fork any repo on github.com. Source: GitHub Guides.

Can I clone a subdirectory in git?

Cloning only a subdirectory is not possible in Git. The network protocol doesn't support it, the storage format doesn't support it. Every single answer to this question always clones the whole repository.

Can you fork a forked repository?

In this model, you fork the project repository to your own account using the GitHub website. You can then clone the forked repository to your desktop as you would any other repo. Typically you would create a working branch on the local copy of the repo, edit the documents, then push the changes to GitHub.


2 Answers

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
    
like image 74
Anshul Goyal Avatar answered Sep 30 '22 03:09

Anshul Goyal


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
    
like image 45
user3756236 Avatar answered Sep 30 '22 02:09

user3756236