Skip to content

Instantly share code, notes, and snippets.

@gildas
Created July 7, 2022 13:24
Show Gist options
  • Save gildas/92223fc584c2e347e5d0dbb8008d7298 to your computer and use it in GitHub Desktop.
Save gildas/92223fc584c2e347e5d0dbb8008d7298 to your computer and use it in GitHub Desktop.
git pull all branches from origin
#
# git pull all from origin
#
function gpuA {
local VERBOSE=${VERBOSE:-0}
while (( $# > 0 )); do
[[ $1 == --*=* ]] && set -- "${1%%=*}" "${1#*=}" "${@:2}"
case $1 in
--quiet)
VERBOSE=0
;;
-v|--verbose)
VERBOSE=$((VERBOSE + 1))
(( VERBOSE > 2 )) && set -o xtrace # trace commands before they are executed
;;
-?*|--*=) # Invalid options
;;
--) # Force end of options
shift
break
;;
*) # Positional Argument
ARGS+=( "$1" )
;;
esac
shift
done
local local_branches=( $(git for-each-ref --format='%(refname:short)' refs/heads/) )
local remote_branches=( $(git branch --remotes --no-color --no-column | grep origin | grep -v HEAD) )
local current_branch="$(git branch --show-current)"
for local_branch in ${local_branches[@]}; do
if [[ -n $(printf "%s|" "${remote_branches[@]}" | grep "origin\/${local_branch}|") ]]; then
(( VERBOSE )) && echo "Pulling branch $local_branch"
if [[ $local_branch == $current_branch ]]; then
git pull
else
git fetch origin "${local_branch}:${local_branch}"
fi
else
(( VERBOSE )) && echo "Not pulling local-only branch $local_branch"
fi
done
}
gpuA "$@"
# vim: set syntax=zsh:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment