Last active
May 14, 2024 16:25
-
-
Save flaxel/fc9f8f721765fb4c41d1db7ccc1b5148 to your computer and use it in GitHub Desktop.
Update all git repositories in one folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you! it's really been useful 👌🏼