How do I git clone --recursive and checkout master on all submodules in a single line?

tedsuo picture tedsuo · Jun 25, 2011 · Viewed 64.3k times · Source

I really like this command to fetch a repo with submodules:

git clone [email protected]:my_user/my_repo.git --recursive

However, the submodules are all set to "no branch" when they arrive, and I have to manually checkout master on each and every one. Is there a way to recursively pull submodules and automatically set the branch to master?

Answer

qbein picture qbein · Sep 25, 2012

After cloning the repository containing your submodules, the following command will checkout the master branch on all of these in one go:

git submodule foreach --recursive git checkout master