Git: how to push submodule to a remote repository?

前端 未结 4 1351
梦谈多话
梦谈多话 2020-12-28 17:04

I use git to track website I\'m working on. I work on my machine and push commits to a remote server configured following this guide: using Git to manage a website.

4条回答
  •  执笔经年
    2020-12-28 17:19

    I had a similar problem: a clone of a repo in a PC (A) with remote in an external website and I wanted to have a clone of my local repo in another PC (B) in the same network where I could push my changes to (through ssh) and make some tests (some of my regression test take a very long time), so that I could keep on working on (A) on an different branch if needed. The repo in (A) has a submodule.

    I created bare repo in (B):

    mkdir /path/to/bare_git && cd /path/to/bare_git
    git --bare init
    

    and added it as a new remote in my local repo in (A):

    git add remote name_of_B_repo ssh://user@host/path/to/bare_git/
    

    and pushed the local repo in (A) (possibly with changes not made public yet) to my ssh repo:

    git push name_of_B_repo branch_to_push
    

    After this, I clone my bare repo from within (B):

    mkdir /path/to/B_clone && cd /path/to/B_clone
    git clone /path/to/bare_git
    git submodule update --remote
    

    and I could see that my submodule was not included.

    Solution 1: If you are not interesting in testing/changing the content of your submodule, but you need it to make your tests, then you can include the external website link directly in the .git/config of (B) clone as:

    [submodule]
            url = http://submodule_external_website_url
    

    then, just update your submodule:

    git submodule update --remote
    

    Solution 2: If you are interested in changing the content of the submodule in (A) and send them to (B), you first need to add the ssh repo in (B) to your local (A) submodule repo as a new remote:

    cd /path/to/submodule
    git add remote name_of_B_repo ssh://user@host/path/to/bare_git/
    

    push your changes to it:

    cd /path/to/main_A_local_repo
    git submodule foreach git push name_of_B_repo branch_to_push
    

    add the submodule local path to the .git/config file of clone repo in (B) as:

    [submodule]
            url = /path/to/bare_git
    

    and update your modules as before:

    git submodule update --remote
    

提交回复
热议问题