Search code examples
git-submodulesprivate-repository

git submodule add private repo in private repo


People ask and ask how git submodule add in case where both repos are private?


Solution

  • This is my example.

    Let's generate key pairs for every private repo.

    user@linux:~/.ssh$ cd $HOME/.ssh && ssh-keygen -t ed25519 -f "myMother_key" -N "" -C "myKeyForGitHubMother" <<< $'\ny' >/dev/null 2>&1
    user@linux:~/.ssh$ cd $HOME/.ssh && ssh-keygen -t ed25519 -f "myBaby_key" -N "" -C "myKeyForGitHubBaby" <<< $'\ny' >/dev/null 2>&1
    

    Via FireFox I open github.com and manually add myMother_key.pub file to [email protected]:AndreiCherniaev/mother.git repo (Allow write access checkbox yes) and myBaby_key.pub to [email protected]:AndreiCherniaev/baby.git (Allow write access checkbox yes)

    I am Using multiple repositories on one server So my ~/.ssh/config file contains

    Host babysubdomain.github.com
      HostName github.com
      User git
      IdentityFile ~/.ssh/myBaby_key
      IdentitiesOnly yes
    
    Host mothersubdomain.github.com
      HostName github.com
      User git
      IdentityFile ~/.ssh/myMother_key
      IdentitiesOnly yes
    

    Where babysubdomain. mothersubdomain. is any text.

    Let's clone baby repo.

    user@linux:~$ git clone [email protected]:AndreiCherniaev/baby.git
    

    Just for fun let's add any public repo as submodule for baby. In my example I will use Buildroot project

    user@linux:~$ cd baby
    user@linux:~/baby$ git submodule add -b master https://github.com/buildroot/buildroot myBuildroot/buildroot
    user@linux:~/baby$ git commit -m "add submodule myBuildroot/buildroot/ to baby"
    user@linux:~/baby$ git push
    

    Let's back to Download/ and clone mother repo

    user@linux:~/baby$ cd ..
    user@linux:~$ git clone [email protected]:AndreiCherniaev/mother.git
    user@linux:~$ cd mother
    

    Let's add submodule to mother repo

    user@linux:~$ cd mother/
    user@linux:~/mother$ git submodule add -b main [email protected]:AndreiCherniaev/baby.git
    user@linux:~/mother$ git commit -m "add submodule baby to mother"
    user@linux:~/mother$ git push
    

    Now let's remove local copy of mother/ from your PC

    user@linux:~/mother$ cd ..
    user@linux:~$ rm -Rf mother/
    

    Let's clone mother with baby with buildroot

    git clone --recurse-submodules -j8 [email protected]:AndreiCherniaev/mother.git
    

    Now we have repo mother with submodule baby with submodule myBuildroot/buildroot/

    Thanks Gubatron and link. Tested on Ubuntu 22.