Skip to content

Instantly share code, notes, and snippets.

@arbales
Created October 13, 2011 22:19
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 arbales/1285717 to your computer and use it in GitHub Desktop.
Save arbales/1285717 to your computer and use it in GitHub Desktop.
`git-bring` brings upstream changes you care about to your local branches.
#!/bin/zsh
CURRENT_BRANCH=$(git symbolic-ref HEAD 2>/dev/null | awk -F/ {'print $NF'})
can_ff() {
a="$(git rev-parse "$1")" &&
test "$(git merge-base "$a" "$2")" = "$a"
}
needs_sync(){
a="$(git rev-list --max-count=1 "$1")" &&
test "$(git rev-list --max-count=1 "$2")" != "$a"
}
git fetch --all
branches=()
eval "$(git for-each-ref --shell --format='branches+=(%(refname:short))' refs/heads/)"
for branch in "${branches[@]}"; do
if (can_ff "$branch" "origin/$branch"); then
if (needs_sync "$branch" "origin/$branch"); then
echo "\n$branch can be fast-forwarded...."
git checkout "$branch"
git rebase "origin/$branch"
else
echo "\n$branch is already up-to-date."
fi
else
echo "\nSkipping $branch..."
fi
done
echo "\nReturning you to $CURRENT_BRANCH..."
git checkout "$CURRENT_BRANCH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment