Created
April 22, 2011 11:35
-
-
Save mhl/936480 to your computer and use it in GitHub Desktop.
An example script to show how to fetch from all default remotes and do any fast-forwards of local branches from upstream possible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fish version: