Skip to content

Instantly share code, notes, and snippets.

@pbyrne
Created August 23, 2012 20:20
Show Gist options
  • Save pbyrne/3441177 to your computer and use it in GitHub Desktop.
Save pbyrne/3441177 to your computer and use it in GitHub Desktop.
Since Git's branch.enablerebase config option only works on branches created after enabling the setting, this iterates over your projects and their branches to enable it for existing branches.
#!/bin/bash
# Enables automatic rebase when pulling for each existing branch in your
# projects. This is useful in conjunction with the 'branch.enablerebase always'
# option, which enables this setting on new branches but leaves existing
# branches intact.
#
# Pass this a directory which contains all of your projects and it will enable
# thie option for all existing local branches in each Git repo within it.
function main() {
if [ -z $1 ]; then
# print usage if not passed a workspace
usage
else
enable_rebase_in_workspace $1
fi
}
function usage() {
echo
echo "enablerebase PROJECTS_WORKSPACE"
echo
echo "Loops through all git repos in PROJECTS_WORKSPACE and enables rebase on all local branches"
echo
echo "Ex:"
echo "enablerebase ~/workspace"
}
function enable_rebase_in_workspace() {
WORKSPACE=$1
echo working in $WORKSPACE
echo
ls $WORKSPACE | while read project; do
enable_rebase_in_project $project
echo
done
}
function enable_rebase_in_project() {
PROJECT=$1
echo working with $WORKSPACE/$PROJECT
cd $WORKSPACE/$PROJECT
if [ -d $WORKSPACE/$PROJECT/.git ]; then
local_branches | while read branch; do
enable_rebase_for_branch $branch
done
else
echo $PROJECT is not a git project, moving along
fi
}
function enable_rebase_for_branch() {
BRANCH=$1
echo looking at $BRANCH of $PROJECT
git config --local branch.$BRANCH.rebase true
}
function local_branches() {
# strip out the leading whitespace and "* " on current branch
git branch | cut -c 3-
}
main $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment