Skip to content

Managing a Full-Stack Application Using Git Submodules

Managing a full-stack application can become complex, especially when multiple teams or individuals are working on different parts of the project. One effective way to handle this complexity is by using Git submodules. This article will guide you through the process of converting parts of your full-stack application into submodules and managing commits effectively.

Identifying Submodules

The first step is to identify which parts of your project you want to convert into submodules. Typically, this could be the frontend, backend, and any other distinct services.

Creating New Repositories

For each part of your project that you want to make a submodule, create a new repository.

Splitting the Existing Repository

Next, split the contents of your existing repository into these new repositories. You can use the git filter-branch command to do this. For example, to split the backend into its own repository:

bash
# Navigate to the existing repository
cd existing-repo

# Create a new branch with only the backend folder
git subtree split --prefix=backend -b split-backend

# Push the new branch to the new backend repository
cd ..
mkdir new-backend-repo
cd new-backend-repo
git init
git pull ../existing-repo split-backend
git remote add origin https://github.com/user/new-backend-repo.git
git push -u origin master

Repeat this process for each part of your project.

Adding Submodules to the Main Repository

After splitting the project, add the new repositories as submodules in the main repository:

sh
# Navigate to the main repository
cd main-repo

# Add the new repositories as submodules
git submodule add https://github.com/user/new-backend-repo.git backend
git submodule add https://github.com/user/new-frontend-repo.git frontend

# Initialize and update the submodules
git submodule init
git submodule update

Managing Commits

When working with submodules, commits are managed separately for each submodule. Here are the common tasks:

Updating a Submodule

To commit changes to a submodule:

sh
# Navigate to the main repository
cd main-repo

# Add the new repositories as submodules
git submodule add https://github.com/user/new-backend-repo.git backend
git submodule add https://github.com/user/new-frontend-repo.git frontend

# Initialize and update the submodules
git submodule init
git submodule update

Updating the Main Repository with Submodule Changes

After committing changes to a submodule, update the main repository to reference the latest commit in the submodule:

sh
# Navigate to the main repository
cd main-repo

# Update the submodule reference
git submodule update --remote backend

# Commit the updated submodule reference in the main repository
git add backend
git commit -m "Update backend submodule"
git push origin master

Tips for Managing Submodules

  • Regularly Update Submodules: Ensure your main repository references the latest commits of your submodules.
  • Document the Process: Clearly document how to clone the repository with submodules and how to update them.
  • Use Branches Consistently: Ensure submodule branches are consistent with the main repository's branches to avoid confusion.

By following these steps, you can effectively manage a full-stack application using Git submodules, ensuring each part of your project can be developed and versioned independently.

Would you like me to create this markdown file for you?