How can I include only one folder from another repository in Git?
Question: I have a python module which looks like this.

    
    
    |
    |-- my_module/
    |-- tests/
    |-- .git/
    

I'd like to use it in another project. Normally submodules would suffice,
however, I'd rather just drop in the actual module without including anything
else into my app like so.

    
    
    |-- .git/
    |-- my_app/
    |-- my_module/
    

Is there a way to only import a single folder using a git submodule?

> If I can't or if it's impractical, how else can I include a single folder
> from another git-tracked project while keeping it version controlled?


Answer: If you wish not to use git `submodule` you can simply checkout the desired
folder.

Since there is no explicit way to grab only folder from the repo you will have
to something manually like this:

    
    
    - create your desired repositories (you already have it)
    - write a script that loop over range of commits
    - extract the desired folder content from the current commit
    - commit the current folder 
    

The problem with this is that you will not have the original SHA-1 since you
only committing partial section of the commit snapshot.

### Code sample

Your code should look something like:

    
    
    for commit in $(git rev-list $branch)
    do
        if git ls-tree --name-only -r $commit | grep '<your desired path>'; then
            // Process the commit content
            git checkout <path>
            git add ....
            git commit .... 
            exit 0
        fi
    done
    

### Why cant I just pull out folder form git history?

The reason is simply the way git store its content.

> Git is stupid content tracker (Linus Tovalds)

Which means that git doesn't store the content in the same way we see it in
our working directory.

Git simply take a **snapshot** of the current file system (In real its little
bit more complicated and git use blobs, hunks, heuristics and much more) so in
order to extract specific content from the history you must `checkout` the
specific content from the the `commit` itself.

