c# – Git submodule best practices: Including specific folder from a multi-project Visual Studio solution


The Problem

I’m developing an integrating solution in Visual Studio 2019 that needs to incorporate several previously developed proof-of-concept prototypes on Azure DevOps. Some of these existing prototypes are:

  1. Single-project repositories (just one .csproj)
  2. Multi-project solutions (multiple .csproj files in one .sln)

For single-project repositories, Git submodules work perfectly. However, I’m struggling with the multi-project solutions where I only need one specific project/folder/module from the repository.

Current approaches I’ve tried:

  1. Using Git submodules for the entire repository:

    MyNewSolution/
    ├── src/
    │   └── externals/
    │       └── ExistingRepo/  # Git submodule containing ALL projects
    │           ├── Project1/
    │           ├── Project2/  # The only one I need
    │           └── Project3/
    
  2. Manual copy of specific project folder:

    MyNewSolution/
    ├── src/
    │   └── externals/
    │       └── Project2/  # Manually copied, losing version control connection
    

The Question

What is the canonical way to include a specific module/folder from a multi-project repository as a git submodule? I want to:

  • Maintain version control connection to the original project
  • Avoid cluttering my new solution with unnecessary projects
  • Follow best practices for both Git and Visual Studio

Minimal Working Example

Original Repository Structure (ExistingRepo):

ExistingRepo/
├── ExistingRepo.sln
├── Project1/
│   └── Project1.csproj
├── Project2/
│   └── Project2.csproj
└── Project3/
    └── Project3.csproj

New Solution Structure (desired):

MyNewSolution/
├── MyNewSolution.sln
└── src/
    ├── MainProject/
    │   └── MainProject.csproj
    └── externals/
        └── Project2/  # Want this as a submodule from ExistingRepo
            └── Project2.csproj

What are the best practices for achieving this structure while maintaining proper version control?

What I’ve Considered

  1. Git sparse checkout (tried unsuccessfully)

    • Pros: Keeps git connection
    • Cons: Seems complex for CI/CD
  2. Breaking up original repository (the one I am moving forward right now)

    • Pros: Clean separation
    • Cons: Major refactoring of existing codebase
  3. Accepting full repository as submodule (don’t like this)

    • Pros: Simple git management
    • Cons: Cluttered solution
  4. NuGet package (don’t know how to do Releases/Artifacts/Piplines on Azure DevOps)

    • Pros: Clean dependency management
    • Cons: Overhead of package management

Leave a Reply

Your email address will not be published. Required fields are marked *