git
git-pull
subdirectories
command-line
duplicate

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:

bash
1find . -type d -name .git -prune | while read -r gitdir; do
2  repo_dir="$(dirname "$gitdir")"
3  echo "Updating $repo_dir"
4  git -C "$repo_dir" pull --ff-only
5 done

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:

bash
1find . -type d -name .git -prune | while read -r gitdir; do
2  repo_dir="$(dirname "$gitdir")"
3
4  if ! git -C "$repo_dir" diff --quiet || ! git -C "$repo_dir" diff --cached --quiet; then
5    echo "Skipping dirty repo: $repo_dir"
6    continue
7  fi
8
9  echo "Pulling clean repo: $repo_dir"
10  git -C "$repo_dir" pull --ff-only
11 done

--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 fetch refreshes remote state without changing local branches.
  • git pull --ff-only allows safe fast-forward updates only.
  • git pull --rebase fits 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:

bash
1find . -type d -name .git -prune | while read -r gitdir; do
2  repo_dir="$(dirname "$gitdir")"
3  echo "== $repo_dir =="
4
5  if git -C "$repo_dir" pull --ff-only; then
6    echo "OK"
7  else
8    echo "FAILED"
9  fi
10 done

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.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions