Skip to content

Instantly share code, notes, and snippets.

@NetzwergX
Created May 5, 2012 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NetzwergX/2604145 to your computer and use it in GitHub Desktop.
Save NetzwergX/2604145 to your computer and use it in GitHub Desktop.
Ever needed to keep your forks in sync with their origin? This will help you for your GIT repositories.
#!/bin/sh
#
# GIT Repository update script by Sebastian Teumert
#
# Stashes local changes, checks out specified branch, pulls branch from origin
# and pushes to upstream.
#
# Used to keep forks synced with their origin
#
# When no argument is specified, this script works in pwd, works on the 'master'-
# branch and uses the git remote 'origin' to pull and 'upstream' to push
# help message
HELP="git-sync [-d|--dir <directory>] [-f] [-b|--branch <branch>] [-o|--origin|--pull <origin>] [-u|--upstream|--push <upstream>] [--filter]";
# default values
wd=$(pwd) # -d -dir
branch=master # -b -branch
force=false # -f -force
pull=origin # -o -origin -pull
push=upstream # -u -upstream -push
filter=*/ # -filter
# read options
while [ $# -gt 0 ]
do
case $1 in
-f) force=true;;
-d|--dir) wd="$2" ; shift;;
-b|--branch) branch="$2" ; shift;;
-o|--origin|--pull) pull="$2" ; shift;;
-u|--upstream|--push) push="$2" ; shift;;
--filter) filter="$2" ; shift;;
-h|--help) echo $HELP; exit;;
(--) shift; break;;
(-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
(*) break;;
esac
shift
done
# switch to dir
cd $wd
FILES="$wd/$filter"
# process repos
for f in $FILES
do
cd "$f"
test -d "$f.git/" && echo "Processing $f ..." || echo "$f ist not a GIT repository"
if [ -d "$f.git/" ];
then
git stash save
git checkout $branch
git remote show | grep $pull > /dev/null && git pull $pull $branch
git remote show | grep $push > /dev/null && git push $push $branch
fi
cd ..
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment