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?
6 Answers
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.
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.
-
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? Jul 11, 2013 at 0:45
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
-
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? Jul 11, 2013 at 0:46
-
@DavidY.Stephenson yes, clustergit will recursively scan a directory (default:
.
) for.git
directories to decide where git repositories live. it then runsgit status
on these. if the status is clean and-p
is specified, it will do agit pull
which will only succeed if a remote has been set up, otherwise a warning will appear (but clustergit will continue with other repositories).– mnagelJul 11, 2013 at 6: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. Jul 12, 2013 at 1: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 doesgit 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.– mnagelJul 12, 2013 at 7:56 -
Awesome. Suggestions: recursive
push
toorigin master
, recursivepull
fromorigin master
, give examples that show-v
turned on and off. Also, is there a way toadd
orcommit
in each directory? Jul 12, 2013 at 14:35
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.
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.
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