Skip to content

Instantly share code, notes, and snippets.

@hmdmph
Last active September 6, 2017 05:40
Show Gist options
  • Save hmdmph/56c7983f144d3e835ab82d2c03c93007 to your computer and use it in GitHub Desktop.
Save hmdmph/56c7983f144d3e835ab82d2c03c93007 to your computer and use it in GitHub Desktop.
Update all git repositories in a folder
#!/bin/bash
# update all git repositories in give folder
COLOR_NONE="\033[0m"
COLOR_1="\033[1;34m"
# making changes to shell options, affects regular expressions
shopt -s extglob nullglob
# check whether 1 arugment passed, if so then use it as base directory, else use current director as base directory
# dont put ending '/'' in folder
basedir="."
if [ -z "$1" ]
then
basedir=$(pwd)
else
basedir="$1"
fi
# check whether 2 arugments passed, if so then use the second one as director to skip
# dont put ending '/' in folder
skipdir=
if [ -z "$2" ]
then
skipdir=""
else
skipdir="$2"
fi
# create array of sub folders
if [[ -z $skipdir ]]; then
folder_array=( "$basedir"/*/ )
else
folder_array=( "$basedir"/!($skipdir)/ )
fi
# going through the sub folder and pull
for i in ${folder_array[@]}; do
if [ -e "$i/.git" ]; then
# go to directory
cd $i > /dev/null
branch=$(git rev-parse --abbrev-ref HEAD);
repo_name=${i/$basedir/""}
echo -e "\n${COLOR_1}Updating ${repo_name%/} - $branch${COLOR_NONE}\n"
# fetch
git fetch
# pull
git pull origin $branch
fi
done
@hmdmph
Copy link
Author

hmdmph commented Sep 5, 2017

update all git repositories in a folder. There are two optional arguments that you can pass while running the script.

Usage ./update-git-repos.sh <folder_path> <folder_name_to_skip>

  • copy the content or download the file update-git-repos.sh
  • chmod +x update-git-repos.sh
  • ./update-git-repos.sh root_folder_path folder_name_to_skip

If no arguments passed it update all git repositories in the current directory without skipping any folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment