Skip to content

Instantly share code, notes, and snippets.

@flaxel
Last active November 21, 2022 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flaxel/fc9f8f721765fb4c41d1db7ccc1b5148 to your computer and use it in GitHub Desktop.
Save flaxel/fc9f8f721765fb4c41d1db7ccc1b5148 to your computer and use it in GitHub Desktop.
Update all git repositories in one folder
#!/bin/sh
# check if console program is installed
check_installation(){
type $1 >/dev/null 2>&1 || { echo >&2 "$1 is required: $2 Aborting."; exit 1; }
}
# printing usage information
usage(){
echo "This program is used to update all git repositories in the given folder."
echo "Usage: $0 <folder>"
exit 1
}
# update a git repository
update(){
# printing action
echo "\nUpdating $1 ..."
# change directory to repository
cd "$1"
# get current branch
current_branch=$(git rev-parse --abbrev-ref HEAD)
# update branch repository
[[ $current_branch =~ ^(master|main)$ ]] && git pull --ff-only --all -p || git pull --ff-only origin $current_branch
# cleanup local branches
git branch --merged | egrep -v "(^\*|master|main)" | xargs git branch -D
}
# check necessary installations
check_installation "git" "Please download git from https://git-scm.com/downloads."
check_installation "realpath" "Please install realpath with 'sudo apt install realpath' or 'brew install coreutils'."
# check if arguments passed
[ $# -eq 0 ] && usage
# printing action
echo "Update all git repositories..."
# absolute origin folder
folder=$(realpath $1)
# update repositories with .git folder
for git_folder in $(find $folder -name '.git'); do update $(dirname "$git_folder"); done
@arielmirra
Copy link

thank you! it's really been useful 👌🏼

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