Skip to content

Instantly share code, notes, and snippets.

@mhl
Created April 22, 2011 11:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhl/936480 to your computer and use it in GitHub Desktop.
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
#!/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
@DimanNe
Copy link

DimanNe commented Dec 14, 2021

Fish version:

# https://gist.github.com/mhl/936480

set -l CURRENT_BRANCH (git symbolic-ref HEAD)
set -l 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
   
for entry in (git for-each-ref --shell --format="%(refname) %(upstream)" refs/heads)
   set -l REF (string trim --chars "'" -- (string split ' ' -- $entry)[1])
   set -l UPSTREAM (string trim --chars "'" -- (string split ' ' -- $entry))[2]
      
   if test "$REF" = "$CURRENT_BRANCH"
      # echo "$REF -> $UPSTREAM: is the current branch => skipping for now"
      continue
   end
   if test -z "$UPSTREAM"
      # echo "$REF -> $UPSTREAM: upstream is absent => skipping for now"
      continue
   end
      
   set -l REF_HASH (git rev-parse --verify $REF)
   set -l UPSTREAM_HASH (git rev-parse --verify $UPSTREAM)
   set -l MERGE_BASES (git merge-base $REF $UPSTREAM)

   if test "$REF_HASH" = "$UPSTREAM_HASH"
      # echo "$REF -> $UPSTREAM: REF_HASH($REF_HASH) is the same as UPSTREAM_HASH => skipping..."
      continue
   end
      
   if test "$MERGE_BASES" = "$REF_HASH"
      echo -e (set_color -o brwhite)Fast-forwarding $REF(set_color normal) -\> $UPSTREAM
      git update-ref "$REF" "$UPSTREAM_HASH"
   else
      echo -e (set_color -o brwhite)Cannot fast-forward: $REF(set_color normal) -\> $UPSTREAM
   end      
end
if test -z "$CURRENT_BRANCH_UPSTREAM"
   # echo "No upstream branch was found for the current branch ($CURRENT_BRANCH)"
else
   echo -e (set_color -o brwhite)Merging $CURRENT_BRANCH(set_color normal) from $CURRENT_BRANCH_UPSTREAM
   git merge $CURRENT_BRANCH_UPSTREAM
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment