Skip to content

Instantly share code, notes, and snippets.

@arosini
Last active March 28, 2016 17:35
Show Gist options
  • Save arosini/b503d1afc3ba5d73ae32 to your computer and use it in GitHub Desktop.
Save arosini/b503d1afc3ba5d73ae32 to your computer and use it in GitHub Desktop.
Script to update Git repositories
#!/bin/bash
# The update_git_repo function updates a local copy of a git repository by fetching/pruning all branches,
# changing to the develop branch, pulling from the remote develop branch and then switching back to the
# original working branch. The first and only argument is the directory of the path to the git repository.
update_git_repo()
{
if [ -d "$1" ]; then
cd $1
export REPO_NAME=$(basename `git rev-parse --show-toplevel`)
echo -n "Updating $REPO_NAME..."
export WORKING_BRANCH=$(git name-rev --name-only HEAD)
git checkout develop > /dev/null 2>&1
git fetch -p > /dev/null 2>&1
git pull > /dev/null 2>&1
git checkout $WORKING_BRANCH > /dev/null 2>&1
echo "complete!"
cd ..
else
echo "Could not update $1 (directory not found)."
fi
}
# Go to location of repos
cd $HOME/projects
# Update Git repos
echo ""
update-git-repo some-repo-name
update-git-repo some-other-repo-name
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment