git clone git@yourcompany.com:team/project.git
cd projectgit remote add public git@github.com:yourusername/project-public.gitCheck remotes:
git remote -v
# origin git@yourcompany.com:team/project.git (fetch)
# origin git@yourcompany.com:team/project.git (push)
# public git@github.com:yourusername/project-public.git (fetch)
# public git@github.com:yourusername/project-public.git (push)git sparse-checkout init --coneExample repo structure:
/src # public source code
/docs # public documentation
/config # private configs
/secrets # private secrets
Set sparse checkout to only public folders:
git sparse-checkout set src docsYour working directory now contains only /src and /docs.
Add and commit changes only to public files:
git add .
git commit -m "Update public files"Push your entire repo (including private files):
git push origin maingit checkout -b public-filtered main
git rm -r --cached config secrets
git commit -m "Remove private files for public repo"
git push -f public public-filtered:maingit switch mainCreate push_public.sh:
#!/bin/bash
# Save current branch
current_branch=$(git branch --show-current)
# Switch to main
git checkout main
# Create or reset public-filtered branch
git branch -D public-filtered 2>/dev/null
git checkout -b public-filtered
# Remove private files
git rm -r --cached config secrets
# Commit changes
git commit -m "Remove private files for public repo"
# Push filtered branch to public remote main branch
git push -f public public-filtered:main
# Switch back to original branch
git checkout "$current_branch"Make it executable:
chmod +x push_public.shRun to sync public repo:
./push_public.sh| Step | Command/Example |
|---|---|
| Clone private repo | git clone git@yourcompany.com:team/project.git |
| Add public remote | git remote add public git@github.com:yourusername/project-public.git |
| Enable sparse checkout | git sparse-checkout init --cone |
| Set sparse folders | git sparse-checkout set src docs |
| Work and commit | git add . && git commit -m "message" |
| Push full private branch | git push origin main |
| Create filtered branch | git checkout -b public-filtered main |
| Remove private files | git rm -r --cached config secrets |
| Commit & push filtered | git commit -m "Remove private files"git push -f public public-filtered:main |
| Switch back to main | git switch main |