Last active
November 13, 2023 00:58
-
-
Save normancapule/d8da53131bfc70fe41e12e2e5b9338fc to your computer and use it in GitHub Desktop.
Bash script to update all git repos inside a parent directory
This file contains 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/bash | |
PROJ_DIRS=( | |
/path/to/parent/work/directory | |
/path/to/another/parent/work/directory | |
) | |
update_git_repo () { | |
cd $1; | |
cd ..; | |
echo "---"; | |
echo $PWD; | |
export CURRENT_BRANCH="$(git symbolic-ref --short HEAD)" | |
export MAIN_BRANCH="$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)" | |
git stash | |
git checkout $MAIN_BRANCH; | |
git fetch; | |
git reset --hard $MAIN_BRANCH; | |
# Delete all merged local branches to the main branch | |
git branch --merged $MAIN_BRANCH | grep -v '^[ *]*master$' | grep -v '^[ *]*main$' | grep -v '^[ *]*staging$' | grep -v '^[ *]*dev$' | grep -v '^[ *]*develop$' | xargs git branch -D | |
git pull | |
git checkout $CURRENT_BRANCH | |
git stash pop | |
} | |
for path in "${PROJ_DIRS[@]}"; do | |
for dir in $(find $path -name .git -maxdepth 2 -type d); do | |
update_git_repo $dir | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment