Skip to content

Instantly share code, notes, and snippets.

@lzap
Last active April 13, 2023 07:48
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 lzap/1ef1709b373ca073ca79538bc76500d2 to your computer and use it in GitHub Desktop.
Save lzap/1ef1709b373ca073ca79538bc76500d2 to your computer and use it in GitHub Desktop.
Script to fetch all branches and fast forward merge master/main/develop without checkout
#!/bin/bash
#UPSTREAM=origin
UPSTREAM=upstream
function bexists() {
local branch=${1}
local existed_in_local=$(git branch --list ${branch})
if [[ -z ${existed_in_local} ]]; then
echo 0
else
echo 1
fi
}
git fetch --all
for BRANCH in main master develop; do
echo "Trying $BRANCH"
if [[ $(bexists $BRANCH) -eq 1 ]]; then
if [[ $(git rev-parse --abbrev-ref HEAD) == "$BRANCH" ]]; then
echo "Merging $UPSTREAM/$BRANCH into the current branch"
git merge --ff-only $UPSTREAM/$BRANCH
else
echo "Merging $UPSTREAM/$BRANCH into $BRANCH"
git fetch $UPSTREAM $BRANCH:$BRANCH
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment