26

I have a git repository that contains other git repositories. Are there commands that recursively push and/or pull for not only the meta-repository but the sub-repositories?

David Y. Stephenson
  • 872
  • 4
  • 22
  • 44
  • Possible duplicate of [Git - easy way pull latest of all submodules](http://stackoverflow.com/questions/1030169/git-easy-way-pull-latest-of-all-submodules). –  Jul 10 '13 at 13:10

6 Answers6

13

I find myself in the same situation whenever I want to update my llvm/clang repositories and with a bit of bash help I can 'git pull' each of them like this:

$> for dir in $(find . -name ".git"); do cd ${dir%/*}; git pull ; cd -; done

This will 'git pull' all git repos found under your current directory, and probably wont work if they are bare repositories.

vguerra
  • 155
  • 1
  • 7
10

Not quite git pull, but close:

git fetch --recurse-submodules

From the Git docs:

--recurse-submodules[=yes|on-demand|no]

This option controls if and under what conditions new commits of populated submodules should be fetched too. It can be used as a boolean option to completely disable recursion when set to no or to unconditionally recurse into all populated submodules when set to yes, which is the default when this option is used without any value. Use on-demand to only recurse into a populated submodule when the superproject retrieves a commit that updates the submodule’s reference to a commit that isn’t already in the local submodule clone.

Community
  • 1
  • 1
  • I'm not (knowingly) referring to submodules. I have a repository that contains other repositories. All of these repositories where initialized by me. Should I turn these somehow into submodules? – David Y. Stephenson Jul 11 '13 at 00:45
9

if you are talking about submodules, see cupcakes answer.

if you are talking about some folder hierarchy containing git repos, you can checkout clustergit, a tool i programmed: https://github.com/mnagel/clustergit

mnagel
  • 6,729
  • 4
  • 31
  • 66
  • Thanks, I'll try this out. Assuming remotes have all been previously set, would I just run 'clustergit -p' in the top directory to pull recursively? – David Y. Stephenson Jul 11 '13 at 00:46
  • @DavidY.Stephenson yes, clustergit will recursively scan a directory (default: `.`) for `.git` directories to decide where git repositories live. it then runs `git status` on these. if the status is clean and `-p` is specified, it will do a `git pull` which will only succeed if a remote has been set up, otherwise a warning will appear (but clustergit will continue with other repositories). – mnagel Jul 11 '13 at 06:25
  • Could you give me an example command to say, push recursively to origin master? I'm having some trouble putting it together from the documentation. – David Y. Stephenson Jul 12 '13 at 01:09
  • @DavidY.Stephenson `clustergit --recursive --remote origin:master --push` you can add `--verbose` to see the actual git commands issued. usually i leave out the `--remote ...` part so it just does `git push`. ps: please update from github, i just pushed a fix to the combination of push/verbose. pps: i will update the docs to include some examples, if you have some suggestions for that, i would be interested. – mnagel Jul 12 '13 at 07:56
  • Awesome. Suggestions: recursive `push` to `origin master`, recursive `pull` from `origin master`, give examples that show `-v` turned on and off. Also, is there a way to `add` or `commit` in each directory? – David Y. Stephenson Jul 12 '13 at 14:35
  • @DavidY.Stephenson i will update the docs. currently there is no support for add/commit as all the current actions are guaranteed to require no user interaction. how would you resolve this? principally it should not be a problem to add add/commit, however. – mnagel Jul 18 '13 at 22:30
  • it would be great if you could, say, recursively add '.', that is the directory contain the .git directory, and recursively git in each repository with a message. – David Y. Stephenson Jul 19 '13 at 02:44
1

I've just write a script to execute recursively on multiple git repositories. You can grab it from here:

https://github.com/DariuszOstolski/rgit

The idea is exactly the same as in clustergit but implementation differs.

Dariusz Ostolski
  • 462
  • 3
  • 10
0

I needed this a while back and made a cli available through npm. https://github.com/kenglxn/gitr/blob/master/README.md

Just do "npm install -g gitr" and then you can do any git command recursively by using gitr.

kenglxn
  • 1,890
  • 2
  • 14
  • 10
0

Verified on git 2.32, there is a command that allows a recursive pull if you set the submodules to track a branch. You can make a submodule track a branch by adding the branch to the .gitmodules file. The .gitmodules file can me modified manually or you can set the submodule branch with the git submodule set-branch command:

git submodule set-branch --branch {BRANCH_NAME} -- {PATH_TO_SUBMODULE}

Replace {PATH_TO_SUBMODULE} with the path to your submodule and {SUBMODULE_BRANCH} with the branch you want the submodule to track. i.e. if I wanted to track my submodule "usbLibrary" to the "main" branch I would:

git submodule set-branch --branch main -- usbLibrary

Now, while I work in this project, I can ensure the usbLibrary submodule is up to date with main by:

git submodule update --remote --recursive

I also like to setup a git alias for this as git pull-subs

git config --global alias.pull-subs 'submodule update --remote --recursive'

Now, any time I need to update everything I just need to:

git pull
git pull-subs
RebelOfBabylon
  • 658
  • 5
  • 12