Skip to content

Instantly share code, notes, and snippets.

@mnem
Created December 6, 2011 14:35
Show Gist options
  • Save mnem/1438396 to your computer and use it in GitHub Desktop.
Save mnem/1438396 to your computer and use it in GitHub Desktop.
Simple bash script for fetching and pulling all repos in the executed folder to the latest of the branch they are on
#!/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
@nikita-fuchs
Copy link

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 !

@khramtsoff
Copy link

khramtsoff commented Dec 12, 2021

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

@extremelogic-ph
Copy link

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

@EliasAfara
Copy link

Thanks

@dscardoso
Copy link

Thank you!

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