Skip to content

Instantly share code, notes, and snippets.

@seanmhanson
Last active November 1, 2019 16:10
Show Gist options
  • Save seanmhanson/b835d9ebcf92e38254ea62fa16429f9e to your computer and use it in GitHub Desktop.
Save seanmhanson/b835d9ebcf92e38254ea62fa16429f9e to your computer and use it in GitHub Desktop.
Custom git command to prefix commit messages with branch and optionally push
#!/bin/sh
while getopts ":ahlp" opt; do
case ${opt} in
h ) echo "Usage: git commit-message [-a] [-h] [-l] [-p] <message>"
echo "[-a] stage all changed files before committing"
echo "[-h] display help and usage (you are here)"
echo "[-l] push using force-with-lease after committing"
echo "[-p] push after committing"
exit 0
;;
a ) stageAll=true
;;
l ) force=true
;;
p ) push=true
;;
\? ) echo "Usage: git commit-message [-a] [-h] [-l] [-p] <message>"
exit 0
;;
esac
done
shift $(($OPTIND - 1))
if currentBranch=$(git symbolic-ref --short -q HEAD)
then
echo On branch "$currentBranch"
if [ "$stageAll" = true ]; then
git add -A
echo "Staged all changed files"
fi
git commit -m "${currentBranch}: ${1}"
if [ "$force" = true ]; then
git push origin "$currentBranch" --force-with-lease
echo "Force pushed with lease to remote branch"
elif [ "$push" = true ]; then
git push origin "$currentBranch"
echo "Pushed to remote branch"
fi
else
echo "ERROR: Cannot find the current branch!"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment