Run git pull over all subdirectories
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Running git pull over many subdirectories is a common maintenance task when you keep multiple repositories under one parent directory. The main challenge is not looping over directories. It is doing so safely, only in actual Git repositories, and without masking failures or trampling dirty working trees. A good solution should make those checks explicit.
Use find and git -C to Target Real Repositories
A reliable shell pattern is locating .git directories and then running Git against the parent directory.
This avoids trying to run git pull in ordinary directories that just happen to sit under the same tree.
Using git -C is cleaner than repeatedly cd-ing in and out of directories because the current shell context stays stable.
Check for Dirty Working Trees First
Blindly pulling every repository is risky if some of them have local changes.
This protects you from surprising merge behavior in repositories that are mid-work.
The --ff-only option is also deliberate. It makes the update fail instead of creating an unexpected merge commit.
Decide Whether You Want Pull, Fetch, or Rebase
git pull is not the only possible operation. Depending on your workflow, you may prefer:
- '
git fetchto refresh remote state without changing local branches' - '
git pull --ff-onlyfor safe fast-forward updates only' - '
git pull --rebaseif your team uses a rebase-based workflow'
The loop structure stays the same. What changes is the operation you run inside it.
For many multi-repo maintenance tasks, fetch is safer as a first pass because it never rewrites the working branch unexpectedly.
Handle Failures Per Repository
One bad repository should not hide the state of the others. Emit clear output and keep going.
This makes the command useful as an operational report rather than a noisy one-liner that leaves you guessing where the problem occurred.
Consider a Dedicated Multi-Repo Tool When the Pattern Is Permanent
If you do this constantly, raw shell loops may not be the best long-term interface. Tooling such as Git submodules, repo managers, or a small project-specific maintenance script can make the workflow more explicit.
The point is not that shell loops are wrong. The point is that repeated operational tasks deserve a shape that matches how often they are used.
Common Pitfalls
- Running
git pullin every subdirectory instead of only actual Git repositories. - Pulling over dirty working trees and triggering conflicts or accidental merges.
- Using plain
git pullwhen--ff-onlyor evenfetchwould be safer. - Writing a loop that stops at the first error and hides the condition of later repositories.
- Treating many unrelated repositories like one project when a stronger multi-repo workflow would be clearer.
Summary
- Use
findplusgit -Cto run Git commands only in real repositories. - Check for local modifications before pulling.
- Prefer safe update modes such as
--ff-onlywhen you automate pulls. - Report failures per repository instead of hiding them in one large loop.
- If the task is permanent, consider a more explicit multi-repo workflow than ad hoc shell commands.

