Skip to content

Instantly share code, notes, and snippets.

@tomaszprasolek tomaszprasolek/git-append
Last active Nov 6, 2018

Embed
What would you like to do?
#!/bin/bash
USAGE="USAGE: git append TEXT_TO_APPEND NUMBER_OF_COMMITS\nExample: git append \"#3301\" 5"
show_usage () {
echo -e "\n$USAGE"
}
case $1 in
help | -h | "/?")
show_usage
exit 1
esac
if [ x = x${1} ] && [ x = x${2} ]; then
echo ">>> Both parameters are empty."
show_usage
exit 1
fi
if [ x = x${2} ]; then
echo ">>> Second parameter is empty."
show_usage
exit 1
fi
# Check if is integer
# https://unix.stackexchange.com/questions/151654/checking-if-an-input-number-is-an-integer
if ! [ "$2" -eq "$2" ] 2> /dev/null
then
echo ">>> Second parameter must be an integer!"
show_usage
exit 1
fi
echo "You want to append text: \"$1\" to last $2 commits."
# https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script
while true; do
read -p "Do you want to continue [y/n]? " yn
case $yn in
[Yy]* ) git filter-branch -f --msg-filter 'sed "s/\(.*\)/\1 '$1'/g"' HEAD~$2..HEAD; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes [y] or no [n].";;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.