Checkout a branch name in all directories

Sat, Dec 14, 2024 One-minute read

We have a bunch of different repos that all have develop as their main branch. Sometimes I am working in various feature braches and then want to checkout out develop in all of them.

Here’s a concise one-liner to switch to the develop branch for each folder under your current working directory:

for dir in */; do [ -d "$dir/.git" ] && (cd "$dir" && git checkout develop); done

This one does a git pull as well:

for dir in */; do [ -d "$dir/.git" ] && (cd "$dir" && git checkout develop && git pull); done