We're using git submodules to manage a couple of large projects that have dependencies on many other libraries we've developed. Each library is a separate repo brought into the dependent project as a submodule. During development, we often want to just go grab the latest version of every dependent submodule.
Does git have a built in command to do this? If not, how about a Windows batch file or similar that can do it?
I think you'll have to write a script to do this. To be honest, I might install python to do it so that you can use
os.walk
tocd
to each directory and issue the appropriate commands. Using python or some other scripting language, other than batch, would allow you to easily add/remove subprojects with out having to modify the script.The following worked for me on Windows.
Note: This is from 2009 and may have been good then but there are better options now.
We use this. It's called
git-pup
:Just put it in a suitable bin directory (/usr/local/bin). If on Windows, you may need to modify the syntax to get it to work :)
Update:
In response to the comment by the original author about pulling in all of the HEADs of all of the submodules -- that is a good question.
I am pretty sure that
git
does not have a command for this internally. In order to do so, you would need to identify what HEAD really is for a submodule. That could be as simple as sayingmaster
is the most up to date branch, etc...Following this, create a simple script that does the following:
git submodule status
for "modified" repositories. The first character of the output lines indicates this. If a sub-repo is modified, you may NOT want to proceed.git checkout master && git pull
. Check for errors.I'd like to mention that this style is not really what git submodules were designed for. Typically, you want to say "LibraryX" is at version "2.32" and will stay that way until I tell it to "upgrade".
That is, in a sense, what you are doing with the described script, but just more automatically. Care is required!
Update 2:
If you are on a windows platform, you may want to look at using Python to implement the script as it is very capable in these areas. If you are on unix/linux, then I suggest just a bash script.
Need any clarifications? Just post a comment.
Remark: not too easy way, but workable and it has its own unique pros.
If one want to clone only
HEAD
revision of a repository and onlyHEAD
s of all the its submodules (i.e. to checkout "trunk"), then one can use following Lua script. Sometimes simple commandgit submodule update --init --recursive --remote --no-fetch --depth=1
can result in an unrecoverablegit
error. In this case one need to clean up subdirectory of.git/modules
directory and clone submodule manually usinggit clone --separate-git-dir
command. The only complexity is to find out URL, path of.git
directory of submodule and path of submodule in superproject tree.Remark: the script is only tested against
https://github.com/boostorg/boost.git
repository. Its peculiarities: all the submodules hosted on the same host and.gitmodules
contains only relative URLs.Git for windows 2.6.3:
git submodule update --rebase --remote
As it may happens that the default branch of your submodules is not
master
, this is how I automate the full Git submodules upgrades: