Last active
November 1, 2019 16:10
-
-
Save seanmhanson/b835d9ebcf92e38254ea62fa16429f9e to your computer and use it in GitHub Desktop.
Custom git command to prefix commit messages with branch and optionally push
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/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