Skip to content

Instantly share code, notes, and snippets.

@reypm
Created October 30, 2023 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reypm/668f56dcec539d70e9fbe4c313a25785 to your computer and use it in GitHub Desktop.
Save reypm/668f56dcec539d70e9fbe4c313a25785 to your computer and use it in GitHub Desktop.
Add branch name to commit message
function commit-with-branch() {
# Get the current branch name
branch_name=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
# Check if we are on a valid branch
if [ -n "$branch_name" ]; then
# Read the commit message from the user
read -e -i "$branch_name: " commit_message
# Remove leading and trailing whitespace from the commit message
commit_message="${commit_message#"${commit_message%%[![:space:]]*}"}"
commit_message="${commit_message%"${commit_message##*[![:space:]]}"}"
# Commit with the branch name at the beginning of the message
git commit -m "$branch_name: $commit_message"
else
echo "Not on a Git branch. Please switch to a branch before committing."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment