-
-
Save mnem/1438396 to your computer and use it in GitHub Desktop.
#!/bin/bash | |
################ | |
# Uncomment if you want the script to always use the scripts | |
# directory as the folder to look through | |
#REPOSITORIES="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
REPOSITORIES=`pwd` | |
IFS=$'\n' | |
for REPO in `ls "$REPOSITORIES/"` | |
do | |
if [ -d "$REPOSITORIES/$REPO" ] | |
then | |
echo "Updating $REPOSITORIES/$REPO at `date`" | |
if [ -d "$REPOSITORIES/$REPO/.git" ] | |
then | |
cd "$REPOSITORIES/$REPO" | |
git status | |
echo "Fetching" | |
git fetch | |
echo "Pulling" | |
git pull | |
else | |
echo "Skipping because it doesn't look like it has a .git folder." | |
fi | |
echo "Done at `date`" | |
echo | |
fi | |
done |
Thanks buddy.
Thank you.
@mnem Can you get me a script similar to this wherein if I am on a different branch other than master, it checks out to master, pulls master, and checkout to my branch and merge master in for all subfolders? Thanks
@a2441918 It should be straightforward for you to add that. Git works with a local database, so all you need to do is update origin/master
and then merge that in - you don't need to change branches. After the git fetch
, that local database is up to date, so you could add something like git merge origin/master
, which will attempt to merge the newly updated origin/master to whatever branch you are on. Of course the merge may fail, so you'd have to decide how you wanted the script to behave in those situations.
Good stuff mate!
Following is one way to have it looping continuously, required a do-while and one additional line of code:
#!/bin/bash
################
# Uncomment if you want the script to always use the scripts
# directory as the folder to look through
#REPOSITORIES="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
i=1
while [ "$i" -ne 0 ]
do
REPOSITORIES=`pwd`
IFS=$'\n'
for REPO in `ls "$REPOSITORIES/"`
do
if [ -d "$REPOSITORIES/$REPO" ]
then
echo "Updating $REPOSITORIES/$REPO at `date`"
if [ -d "$REPOSITORIES/$REPO/.git" ]
then
cd "$REPOSITORIES/$REPO"
git status
echo "Fetching"
git fetch
echo "Pulling"
git pull
cd ..
else
echo "Skipping because it doesn't look like it has a .git folder."
fi
echo "Done at `date`"
echo
fi
done
sleep 10
done
Runs every 10 seconds, change the number in sleep 10
according to your needs. make it executable with sudo chmod +x yourFIleName.sh
, then run it with $ ./yourfileName.sh
- cool hack: If you start a screen you can have it running in the background: screen -S autopuller
and then run the script. To detach from the screen, press Ctrl + A and Ctrl + D !
Recursive version of script
#!/bin/bash
################
sync_directory() {
IFS=$'\n'
for REPO in `ls "$1/"`
do
if [ -d "$1/$REPO" ]
then
echo "Updating $1/$REPO at `date`"
if [ -d "$1/$REPO/.git" ]
then
git -C "$1/$REPO" pull
else
sync_directory "$1/$REPO"
fi
echo "Done at `date`"
fi
done
}
REPOSITORIES="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
sync_directory $REPOSITORIES
I have a similar one, but I think the difference is that mine supports default branches
https://github.com/extremelogic-ph/xl_scripts/blob/master/xl_sync_git.sh
Thanks
Thank you!
Thanks!!!!!!
Very useful, thanks