Skip to content

Instantly share code, notes, and snippets.

@ahogen
Created August 2, 2017 20:57
Show Gist options
  • Save ahogen/0481ed32cad93645cc4e5c758a3896bd to your computer and use it in GitHub Desktop.
Save ahogen/0481ed32cad93645cc4e5c758a3896bd to your computer and use it in GitHub Desktop.
#!/bin/bash
###############################################################################
# File: pull_all_git_branches.sh
# Author: Alex Hogen (@ahogen on Github)
#
# Iterate through all cloned git repositories in this directory and update
# (pull) all their branches to sync with their remote counterparts.
#
# In diagram below, if this script is executed from <this dir>, then all repositories
# in the subdirectories will be updated.
#
# <this dir>
# |
# --- git_repo_1
# |
# --- git_repo_2
# |
# --- git_repo_3
###############################################################################
# https://stackoverflow.com/questions/4000613/bash-for-each-directory
for D in *; do
if [ -d "${D}" ]; then
echo "${D}" # your processing here
cd "${D}"
# https://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
cd -
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment