public

An example script to show how to fetch from all default remotes and do any fast-forwards of local branches from upstream possible

  • Download Gist
git-fetch-and-fast-forward-all-branches.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#!/bin/sh
 
set -e
 
CURRENT_BRANCH="$(git symbolic-ref HEAD)"
CURRENT_BRANCH_UPSTREAM="$(git rev-parse --symbolic-full-name @{u} 2> /dev/null)"
 
# Update the remote-tracking branches for every remote for which
# remote.<name>.skipDefaultUpdate is not true:
git remote update default
 
git for-each-ref --shell --format="REF=%(refname); UPSTREAM=%(upstream)" \
refs/heads | while read entry
do
eval "$entry"
 
echo "Considering $REF ..."
 
# Deal with the current branch last:
if [ "$REF" = "$CURRENT_BRANCH" ]
then
echo " Skipping the current branch for now."
continue
fi
 
if [ x"$UPSTREAM" = x ]
then
echo " No upstream branch is set for $REF"
continue
fi
 
REF_HASH="$(git rev-parse --verify $REF)"
UPSTREAM_HASH="$(git rev-parse --verify $UPSTREAM)"
MERGE_BASES="$(git merge-base $REF $UPSTREAM)"
 
if [ x"$REF_HASH" = x"$UPSTREAM_HASH" ]
then
echo " The branch is already at the upstream version"
continue
fi
 
# Now test if the branch can be fast-forwarded:
if [ x"$MERGE_BASES" = x"$REF_HASH" ]
then
echo " Fast-forwarding $REF from $UPSTREAM"
echo " $REF_HASH -> $UPSTREAM_HASH"
git update-ref "$REF" "$UPSTREAM_HASH"
else
echo " Cannot fast-forward $REF with $UPSTREAM"
fi
 
done
 
if [ x"$CURRENT_BRANCH_UPSTREAM" = x ]
then
echo "No upstream branch was found for the current branch ($CURRENT_BRANCH)"
else
echo "Now deal with the current branch:"
echo "While on $CURRENT_BRANCH, merging from $CURRENT_BRANCH_UPSTREAM"
git merge $CURRENT_BRANCH_UPSTREAM
fi

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.