Created
November 8, 2019 05:17
-
-
Save ivanmorenoj/6ae9c5309fe690165428bd9e678209e3 to your computer and use it in GitHub Desktop.
Update all git repos in one folder
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/sh | |
# | |
# Author: Ivan Moreno | |
# Novemver 2019 | |
DIRS=(~/WorkSpace) | |
for folder in ${DIRS[@]}; do | |
echo ">>>>>>>>>>>>>>>>> Find git repos in $folder" | |
find $folder -name .git -type d -prune | while read dir; do | |
cd $dir/.. | |
echo ">>>>>>>>>>>>> In $PWD" | |
git_save=0 | |
git_branch=0 | |
current_branch=$(git branch | grep \* | cut -d ' ' -f2) | |
status=$(git status | grep "nothing to commit, working tree clean") | |
if [[ $(git remote show) ]]; then | |
if [[( $status != "nothing to commit, working tree clean")]]; then | |
echo ">>>> uncommited changes, saving changes" | |
if git stash save --include-untracked "stash in $(date)" | grep -q "No local changes to save"; then | |
echo ">>>> no changes to save" | |
else | |
git_save=1 | |
fi | |
fi | |
if [[ ( $current_branch != "master" ) ]]; then | |
echo ">>>> current repo is in $current_branch" | |
echo ">>>> changing to master" | |
git checkout master | |
git_branch=1 | |
fi | |
echo ">>>> update from remote repo" | |
git pull | |
if [ $git_branch -eq 1 ]; then | |
echo ">>>> checkout to initial branch: $current_branch" | |
git checkout $current_branch | |
fi | |
if [ $git_save -eq 1 ]; then | |
echo ">>>> unstashing changes" | |
git stash pop | tail -n 2 | |
fi | |
else | |
echo ">>>> no remote found" | |
fi | |
echo -e "\n" | |
cd $OLDPWD | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment