Skip to content

Instantly share code, notes, and snippets.

@mnem
Created December 6, 2011 14:35
Show Gist options
  • Star 67 You must be signed in to star a gist
  • Fork 37 You must be signed in to fork a gist
  • 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
@Warafux
Copy link

Warafux commented Jan 26, 2018

Very useful, thanks

@arunmmanoharan
Copy link

Thanks buddy.

Copy link

ghost commented Dec 1, 2019

Thank you.

@arunmmanoharan
Copy link

@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

@mnem
Copy link
Author

mnem commented Dec 1, 2019

@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.

Copy link

ghost commented Dec 12, 2019

Good stuff mate!

@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