Search code examples
gitgit-remote

How to point a subdirectory of a git repo to a separate remote repo?


I've got an existing project in a repository with a single remote. I'm trying to add another remote, but I need the new remote to only be associated with a subdirectory.

/project/db
/project/misc
/project/app
/project/Dockerfile
/project/README.md

Given a project structure like the above, I want to be able to have /project/app connected to a new remote repository, so that if I push to that repository, it treats /app as the root. Is this even possible? I know I could just create a new local repo and copy the files from /app into it, but I'd like to keep my project history, and keep the other files (which are related) with the /app files.

The reason I'm doing this is because I had to switch from custom hosting, where I controlled the site root, to a different provider (WPEngine) with a more strict setup where the app files need to be in the repository root.


Solution

  • What you're looking for are Git submodules.

    They allow you to use repositories within repositories, each with its own separated history.

    All the super repository can see is the change in the checked out commit of the child, and by committing that you get a link between the two (all further clones of the parent will include that specific version of child).