Skip to content

Instantly share code, notes, and snippets.

@soutar
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save soutar/ff701834d0ce6194f677 to your computer and use it in GitHub Desktop.

Select an option

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

soutar commented Dec 3, 2014

Copy link
Copy Markdown
Author

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