Skip to content

Instantly share code, notes, and snippets.

@soutar
Last active August 29, 2015 14:10
Show Gist options
  • Save soutar/ff701834d0ce6194f677 to your computer and use it in GitHub Desktop.
Save soutar/ff701834d0ce6194f677 to your computer and use it in GitHub Desktop.
Pull all repos in the current directory
#!/bin/bash
stash_name="wip"
echo "> Updating master on all git repositories in the current directory"
for dir in */
do
if [ -d ${dir}/.git ]; then
cd ${dir}
stashed=false
echo "----------------------------------------------------"
echo "- Processing ${dir}"
echo "----------------------------------------------------"
current_branch=$(git symbolic-ref --short HEAD)
# If git status has any output then we need to stash our changes
# so we can restore them after
if [[ -n $(git status -s) ]]; then
echo "> Saving WIP changes"
git stash save ${stash_name}
stashed=true
fi
# If we're not on master, checkout master
if [[ ${current_branch} -ne "master" ]]; then
echo "> Checking out master"
git checkout master
fi
echo "> Pulling master"
git pull origin master
# If we've moved branch, checkout the branch we started on
if [[ $(git symbolic-ref --short HEAD) -ne ${current_branch} ]]; then
git checkout ${current_branch}
fi
# If we stashed changes previously, restore them
if [[ ${stashed} = true ]]; then
echo "> Restoring WIP changes"
git stash pop stash^{/${stash_name}}
fi
cd - > /dev/null # Leave the repo
fi
done
@dunknicoll
Copy link

Only risk i see with this is if one of your repos isnt on master / there are changes to files. Maybe add checking for that?

@soutar
Copy link
Author

soutar commented Dec 3, 2014

Good idea. I'll see what I can hack together

@austintino
Copy link

Also, maybe some command line flags to update all, api, cms, core etc.

array=( $@ )
len=${#array[@]}

if [[ $len -eq 1 ]] && [[ "$@" == "all" ]]
then
echo "INFO: updating ALL sites.
else
echo "INFO: updating the given list [$@]"
cd ~/Sites
for site in $@ ; do

@danielhouriez
Copy link

If the index is dirty, you can always stash non-committed changes before pulling and pop them back once it's done

@danielhouriez
Copy link

There's another issue I tried to cater for with a script, which I unfortunately haven't had much time to look into. If you're switching between projects, your repos may be on different branches, e.g., master, feature1, and feature2, and it's easy to forget. That's where Brendan's flags could come in handy, but you still need to remember what branch each of your repos is on.

@dunknicoll
Copy link

I'm ok with stashing if it's automatically put back on, but still wary here. The other thing is as mentioned all the various combos of, is the index dirty, is the repo on master (obviously it shouldnt really be dirty on master, but we all do that sometimes by mistake and then create a branch later). If its on another branch should it update that branch or should it update master? (this could be a parameter to pass ๐Ÿ˜„ ). These aren't problems or anything preventing from working, just things to take into account and features to add i guess ๐Ÿ˜„

@soutar
Copy link
Author

soutar commented Dec 3, 2014

I've added some more bits to the original gist but it still accepts no parameters. It should now stash any changes you have, checkout the master branch if not already checked out, pull from origin and restore your branch & changes once finished

Me

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