Run git pull in all subdirectories (all repos in a folder)
Codemia Knowledge Hub ·
To pull every repository under a folder, find the .git directories and run git -C <repo> pull --ff-only on each parent directory. The loop itself is easy. The parts worth getting right are touching only real repositories, skipping ones with local changes, and reporting each failure instead of hiding it.
Use find and git -C to target real repositories
Locate .git directories, then run Git against the directory that contains each one:
This never runs git pull in ordinary directories that happen to sit in the same tree. git -C is also cleaner than repeatedly cd-ing in and out, because the shell's current directory never changes.
Skip repositories with local changes
Pulling over a working tree that is mid-work can trigger conflicts or surprise merges. Check for staged and unstaged changes first:
--ff-only is deliberate: the update fails instead of creating an unexpected merge commit.
Choosing between pull, fetch and rebase
The loop stays the same whatever you run inside it:
git fetchrefreshes remote state without changing local branches.git pull --ff-onlyallows safe fast-forward updates only.git pull --rebasefits a team that uses a rebase-based workflow.
For many multi-repo maintenance tasks, fetch is the safer first pass because it never rewrites the working branch.
Report failures per repository
One bad repository should not stop the loop or hide the state of the others. Print a clear result for each one and keep going:
The output then works as a report showing exactly which repository failed.
When to use a dedicated multi-repo tool
If you do this constantly, a raw shell loop may not be the best long-term interface. Git submodules, a repo manager, or a small project-specific maintenance script make a permanent workflow more explicit. Treating many unrelated repositories as one project with ad hoc loops gets harder to follow as the set grows.
Related reading
- running git clone against AWS CodeCommits gets me a 403 error
- Running PowerShell scripts as git hooks
- Search all of Git history for a string
- Search code inside a Github project
- Secure access to a private helm repository
- See changes to a specific file using git
- See diff between current state and last commit
- See what's in a stash without applying it
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.