← Back to list

How I Used Git Submodules to Safely Share Private Code in a Public Repo

“ When working on something open to the world, you don’t always need to reveal everything.”

Tawfiqur Rahman · 2025-07-01 10:03 · 0 claps · 1.6 min read
#git #android #submodules
Open on Medium ↗
Wiki topics: 🔓 · Open Source

How I Used Git Submodules to Safely Share Private Code in a Public Repo

“ When working on something open to the world, you don’t always need to reveal everything.”

A Short Story from My Desk

While working on **Kahf Browser**, an open-source browser designed with privacy in mind, I faced a challenge. We had some private code — like the core strength of the Browser — that we didn’t want to expose in our public repo.

The public GitHub repository was ready to go, but that sensitive piece had to stay hidden.

That’s when I turned to Git submodules — a clean, version-controlled way to include private components in public projects.

If you’re also juggling private code in a public Android app, here’s how you can do it too.

What Is a Git Submodule?

A Git submodule lets you embed one Git repository inside another. This is ideal when:

  • You want to reuse shared code across multiple projects.
  • You want to keep private code separate from your open-source project.
  • You want version control over that external code.

Step-by-Step Guide: How to Add a Private Submodule to a Public Repo

  1. Make sure your private code is in a separate Git repo
git@github.com:your-org/private-library.git
  1. Add the Private Repo as a Submodule in your public Android project
git submodule add git@github.com:your-org/private-library.git libs/private-library

This will:

  • Clone the private repo into libs/private-library
  • Create a .gitmodules file with the submodule config
  • Stage the submodule folder and .gitmodules for commit
  1. Commit the Submodule Configuration. Don’t worry, it won't commit the code - just the configuration.
git add .gitmodules libs/private-library
git commit -m "Add private-library as a submodule"
  1. Integrate the Library into Your Android Project in settings.gradle
include ':app'
include ':private-library'
project(':private-library').projectDir = file('libs/private-library')

Then add the dependency in the app-level build.gradle file.

dependencies {
    implementation project(":private-library")
}

Now, the private code is part of your build, without exposing its content publicly.

Cloning the Project

If someone else is cloning the project:

git clone --recurse-submodules git@github.com:your-org/public-project.git

If they have already cloned it:

git submodule update --init --recursive

They must have access to the private repo, or the submodule will fail to load.

Updating the Submodule

After modifying the submodule codes, navigate to the submodule folder:

cd libs/private-library
git add *
git commit -m "Update private-library submodule"

Have any questions or tips of your own? Drop them in the comments. Thanks!


메타데이터
post_id
d5f4b5f60128
slug
how-i-used-git-submodules-to-safely-share-private-code-in-a-public-project-d5f4b5f60128
url
https://medium.com/@xiifwat/how-i-used-git-submodules-to-safely-share-private-code-in-a-public-project-d5f4b5f60128
canonical_url
https://medium.com/@xiifwat/how-i-used-git-submodules-to-safely-share-private-code-in-a-public-project-d5f4b5f60128
author_url
https://medium.com/@xiifwat
status
ok
fetched_at
2026-07-06 19:56:14