ssk
ssk

Reputation: 9275

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.

Upvotes: 1

Views: 1475

Answers (2)

ashwinik001
ashwinik001

Reputation: 5183

I wonder why with git-submodule it could not be achieved. As I remember, with submodules it is possible to specify the destination directory and hence probably you can do something like this:

$ cd Android
$ git submodule add url/path-to-your-submodule-repo VolumeControl
$ git diff --cached --submodule # OPTIONAL - To verify before commit
$ git commit -m 'added multimedia/VolumeControl as sub-module'

Upvotes: 0

CodeWizard
CodeWizard

Reputation: 142662

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

Upvotes: 4

Related Questions