Skip to content

Instantly share code, notes, and snippets.

@Glutexo
Last active August 29, 2015 14:11
Show Gist options
  • Save Glutexo/6904dbac131fcd5a67fa to your computer and use it in GitHub Desktop.
Save Glutexo/6904dbac131fcd5a67fa to your computer and use it in GitHub Desktop.
GIT: Creates a cumulative patch from the specified commit to the current HEAD.
#!/bin/sh
# Won’t work if a branch called _squash_patch_tmp already exists.
# And probably many other caveats?
REFS_NAMED_HEAD=`git show-ref --heads --tags | grep "^[0-9a-f]\{40\} refs/\(heads\|tags\)/HEAD$"`;
if [ "$REFS_NAMED_HEAD" ]; then
echo "HEAD is ambiguous.";
exit 2
fi
if [ -z "$1" ]; then
echo "Syntax: squash-patch.sh FROM"
exit 1
fi
FROM_COMMIT=$1
git merge-base --is-ancestor "$FROM_COMMIT" HEAD
if [ $? -ne 0 ]; then
echo "$FROM_COMMIT is not an ancestor of the current HEAD."
exit 1
fi
# We want to return to the commit we were originally on. If we were on branch,
# return to that by name, not just to the commit hash.
BASE=`git rev-parse --abbrev-ref HEAD`
if [ "$BASE" = "HEAD" ]; then
BASE=`git rev-parse HEAD`
fi
TMP_BRANCH=_squash_patch_tmp
set -x
git checkout "$FROM_COMMIT"
git checkout -b "$TMP_BRANCH"
git merge --squash "$BASE"
GIT_EDITOR=true git commit
git format-patch HEAD^
git checkout "$BASE"
git branch -D "$TMP_BRANCH"
set +x
ls *.patch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment