Skip to content

Instantly share code, notes, and snippets.

@ScreamingDev
Last active December 26, 2015 01:09
Show Gist options
  • Select an option

  • Save ScreamingDev/7069261 to your computer and use it in GitHub Desktop.

Select an option

Save ScreamingDev/7069261 to your computer and use it in GitHub Desktop.
Unfold changes into "deeper" branches
#!/bin/bash
# if you have branches like
# - dev-foo
# - dev-foo-bar
# - dev-foo-bar-baz
#
# then go in dev-foo and run "git unfold":
# - merges the origin branch (dev-foo) into all the others (dev-foo-bar, dev-foo-bar-baz)
# - fail safe: on conflicts it will undo the merge
# - no magic: you need to push by hand of course
echo "this needs all changes to be stashed or commited!"
echo ""
origin_branch=$(git rev-parse --symbolic-full-name HEAD)
origin_branch_short=`echo ${origin_branch} | sed -e "s/refs\/heads\///g"`
for target_branch in $(git for-each-ref --format='%(refname)' refs/heads/);
do # unfold on existing target_branch
if [[ ${target_branch} =~ ^${origin_branch}.* ]] \
&& [[ "${target_branch}" != "${origin_branch}" ]]; then # we found a sub-target_branch
# refs/heads/foo -> foo
target_branch_short=`echo ${target_branch} | sed -e "s/refs\/heads\///g"`;
echo "Merge ${origin_branch_short} into ${target_branch_short} ...";
git checkout ${target_branch_short} -q
# merge
fallback_hash=$(git rev-parse HEAD);
merge_status=$(git merge ${origin_branch_short} -q);
if [[ ${merge_status} -ne 0 ]]; then # the merge failed: reset it
echo -n "FAILED to merge into ${target_branch_short}";
# fall back
if [[ $(git reset --hard ${fallback_hash} -q) -eq 0 ]]; then # it's restored
echo " - has been reseted.";
else # our fallback failed
echo " - HAS NOT BEEN RESETED!";
exit 1;
fi
fi
fi;
done
# hop back
git checkout ${origin_branch_short} -q
echo "";
echo "Remember to push: git push --all";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment