Last active
August 29, 2015 14:27
-
-
Save samueljackson92/8a91439355d3411d9253 to your computer and use it in GitHub Desktop.
Bash git script to revert n pull requests from a git repo. The command will squash each of the reverts into a single commit to keep the history cleaner. A commit message must also be supplied as an argument.
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/bash | |
usage="$(basename "$0") [-h] <n> <commit-message> -- revert back n pull requests | |
where: | |
-h show this help text" | |
while getopts ':h:' option; do | |
case "$option" in | |
h) echo "$usage" | |
exit | |
;; | |
:) printf "missing argument for -%s\n" "$OPTARG" >&2 | |
echo "$usage" >&2 | |
exit 1 | |
;; | |
\?) printf "illegal option: -%s\n" "$OPTARG" >&2 | |
echo "$usage" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
#check if the user is being silly | |
if [ $# -ne 2 ] | |
then | |
echo "Command requires at exactly two arguments" | |
echo "$usage" >&2 | |
exit 1 | |
fi | |
#check if we have valid number | |
if echo $1 | grep -E '^[0-9]+$' > /dev/null | |
then | |
output="$(git rev-list --merges --max-count=$1 HEAD)" | |
else | |
echo "Argument must be a positive integer." | |
echo "$usage" >&2 | |
exit 1 | |
fi | |
#convert list of merge hashes to array | |
read -a commits <<< $output | |
#Revert each o the changes | |
for commit in "${commits[@]}"; do | |
echo Reverting merge: $commit | |
git revert -m 1 --no-edit $commit | |
done | |
#Squash the reverts into a single commit for cleaner history | |
git reset --soft HEAD~$1 | |
message="The following merge commits were reverted: ${commits[@]}" | |
git commit -m "$2" -m "$message" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment