Pushing all submodules recursively

前端 未结 4 1805
终归单人心
终归单人心 2021-02-06 05:53

I wrote the below script to push all the changes in the workspace, both in the submodules and the superproject. However, it sounds a little odd that, it is this complex to do wh

4条回答
  •  天涯浪人
    2021-02-06 06:05

    The command git push --recurse-submodules=on-demand does not work if you have submodules which contain submodules. (git version: 2.20.1)

    Add the following alias in your ~/.gitconfig file:

    [alias]
        push-all = "! find . -depth -name .git -exec dirname {} \\; 2> /dev/null | sort -n -r | xargs -I{} bash -c \"cd {}; git status | grep ahead > /dev/null && { echo '* Pushing: {}'; git push; }\""
    

    Then issue git push-all in your parent git folder.

    Explanation

    • !: We are issuing a non-git command.
    • find . -depth -name .git -exec dirname {} \\; 2> /dev/null : Find all submodules (and git repositories, which wouldn't harm)
    • | sort -n -r: Sort by path depth, deepest will be first.
    • | xargs -I{} bash -c \": Pass directories to the following commands:
      • cd {};: cd to the target directory.
      • git status | grep ahead > /dev/null: Test if it is necessary to push this repository.
      • && { echo '* Pushing: {}'; git push; }\"": Inform and push.

提交回复
热议问题