gitgit-submodulessvn-externalsgit-sparse-checkout

Checkout a Specific Folder within a Git Repository


I have multiple projects in different repositories:

tools

thirdparty

shared
├──Common
├──Exceptions

multimedia
├──VolumeControl
├──VideoRenderer

android
├──Audio

For example, android depends on shared/Common, shared/Exceptions, tools, thirdparty and multimedia/VolumeControl.

I tried:

  1. Submodules:https://git-scm.com/book/en/v2/Git-Tools-Submodules
  2. sparse-checkout:http://jasonkarns.com/blog/subdirectory-checkouts-with-git-sparse-checkout/

However, they both checkout or bring-in multimedia folder within the android folder. Instead, I want it like:

android
├──Audio
├──VolumeControl (brought in from multimedia)

How do I accomplish this?

Note: I used to accomplish this using lockexterns in svn.


Solution

  • How do I accomplish this?

    You can use git filter-branch and or git subtree split

    filter-branch will loop over each commit and then you can checkout only the given files while git subteree split will be a better option in your case.

    Explaining both of the option so you can choose which one you prefer.


    Sample code:

    filter-branch

    # Filter the master branch to your directory and remove empty commits
    git filter-branch --prune-empty --subdirectory-filter YOUR_FOLDER_NAME filter_from_branch
    

    This will checkout all your desired files from the given folder to the current directory


    subtree split

    git subtree git-subtree - Merge subtrees together or split repository into subtrees

    git subtree split -P <name-of-folder> -b <name-of-new-branch>
    

    enter image description here