Skip to content

Instantly share code, notes, and snippets.

@juanonsoftware
Last active November 22, 2023 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanonsoftware/4453ec06799cdf8ae93a999d2804d9ea to your computer and use it in GitHub Desktop.
Save juanonsoftware/4453ec06799cdf8ae93a999d2804d9ea to your computer and use it in GitHub Desktop.
Git Submodule practices
To add other repos as submodules in an existing git repo
1. Create a public repo for submodule https://github.com/juanonsoftware/PocGitAsPublicSubmodule, with some content.
2. Checkout the main public repo https://github.com/juanonsoftware/PocGitWorkflows
3. Run a command to add the child-repo as a submodule
git submodule add -f https://github.com/juanonsoftware/PocGitAsPublicSubmodule.git PublicSubmodule
4. If any error, delete the folder in parent repo and in .git/module
When success, it will say:
Cloning into 'D:/Dev/PocGitWorkflows/PublicSubmodule'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
warning: in the working copy of '.gitmodules', LF will be replaced by CRLF the next time Git touches it
5. Git commit and git push to update public repo
6. Now work on submodule's repo, push new content
7. Back to main repo, pull / or view on Git, nothing updated. Because the submodule is reference to a specific commit
8. Now to update the submodule, we run below command:
git submodule update --remote
Output will look like this:
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 272 bytes | 20.00 KiB/s, done.
From https://github.com/juanonsoftware/PocGitAsPublicSubmodule
41bb389..a6e3ee1 master -> origin/master
Submodule path 'PublicSubmodule': checked out 'a6e3ee179e377f04303d8d5f6019bb1991116e8e'
9. Add the change, commit, push to public repo for other to get it
10. We can view on Github also
@juanonsoftware
Copy link
Author

juanonsoftware commented Nov 21, 2023

To create private submodule

  1. Create a private repo https://github.com/juanonsoftware/PocGitAsPrivateSubmodule

  2. Commit some data to private repo

  3. Repeat the steps to init private submodule
    git submodule add -f https://github.com/juanonsoftware/PocGitAsPrivateSubmodule.git PrivateSubmodule

  4. Commit and push

  5. So now on Github, other users cannot see the private submodule

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment