#!/bin/bash | |
# This way you can customize which branches should be skipped when | |
# prepending commit message. | |
if [ -z "$BRANCHES_TO_SKIP" ]; then | |
BRANCHES_TO_SKIP=(master develop test) | |
fi | |
BRANCH_NAME=$(git symbolic-ref --short HEAD) | |
BRANCH_NAME="${BRANCH_NAME##*/}" | |
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$") | |
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1) | |
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then | |
sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1 | |
fi |
This comment has been minimized.
This comment has been minimized.
Updated script to not include [master] in the commit message when working on this branch. |
This comment has been minimized.
This comment has been minimized.
Updated script to use better commands based on stackoverflow hints |
This comment has been minimized.
This comment has been minimized.
Script handles now branching name which is for example used in git-flow. Eg for branch 'feature/ABC-399' it will extract ABC-399 |
This comment has been minimized.
This comment has been minimized.
Thanks, man. This is exactly what I need it. |
This comment has been minimized.
This comment has been minimized.
Can the "skipped" branches include wildcards? Thanks for the script ;) |
This comment has been minimized.
This comment has been minimized.
Tried to write this myself after leaving NEO. Your solution is way nicer than mine :) |
This comment has been minimized.
This comment has been minimized.
Thanks for sharing this! Others who only want this script to run when
Prevents the branch code from being triggered if any parameter was passed to |
This comment has been minimized.
This comment has been minimized.
Thanks for this hook. |
This comment has been minimized.
This comment has been minimized.
One important note (which occurred to me too) - |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@r3dDoX it's mature and refined several times :) Glad to see you around! |
This comment has been minimized.
This comment has been minimized.
Cheers mate! :) |
This comment has been minimized.
This comment has been minimized.
Neat little gist |
This comment has been minimized.
This comment has been minimized.
For a quick install, into your project folder enter this command line:
|
This comment has been minimized.
This comment has been minimized.
Finally a hook that works! Thanks a lot for this. |
This comment has been minimized.
This comment has been minimized.
I have many repos. Is there a way to do this at a higher level, instead of one repo at a time? |
This comment has been minimized.
This comment has been minimized.
What would I need to change to get a complete branch name in the commit message? |
This comment has been minimized.
This comment has been minimized.
Thank you. It is very useful! |
This comment has been minimized.
This comment has been minimized.
@gezzi you can paste this bash script into a hooks folder in your directory of choice. you can then run 'git config --global core.hooksPath /path/to/my/centralized/hooks' where /path/to/my/centralized/hooks is the hooks folder you created containing this script. read more about it here: https://stackoverflow.com/questions/2293498/git-commit-hooks-global-settings |
This comment has been minimized.
This comment has been minimized.
I have added this script in mac os 10.13.1 and it show the branch name appenede when do |
This comment has been minimized.
This comment has been minimized.
I had a similar use case, but instead of skipping certain branches I needed to only include branches that start with certain characters. Here is my version of the hook: https://gist.github.com/shevaroller/680719f31e610cff3e6d8c930a078eb0 |
This comment has been minimized.
This comment has been minimized.
Thanks man! Note to other newbies like me:
|
This comment has been minimized.
This comment has been minimized.
Nice one! Thanks! |
This comment has been minimized.
This comment has been minimized.
Amazing! |
This comment has been minimized.
This comment has been minimized.
This is great, thanks a lot! |
This comment has been minimized.
This comment has been minimized.
Does anyone here uses GitKraken? I tried to use this hook but it isn't appending the branch name to the commit when commiting from GitKraken. |
This comment has been minimized.
This comment has been minimized.
Hello, I must be doing something wrong. I'm trying to use your script, as is. But when I try to commit, I get the following error: ": bad flag in substitute command: 'E'
Any thoughts? thanks |
This comment has been minimized.
This comment has been minimized.
https://github.com/milin/giticket might help if you are using precommit. |
This comment has been minimized.
This comment has been minimized.
@rsirani: This works on macOS (tested on Mojave): (Edit: My script below was based on an old version of this Gist)
It will only include the Also, it will only append the branch name if it follows that patter ( This does mean if your ticket system follows a different convention (e.g., it doesn't begin with uppercase letters), this won't append the ticket number. But this works well for people using JIRA. |
This comment has been minimized.
This comment has been minimized.
did some modifications a while ago that:
|
This comment has been minimized.
This comment has been minimized.
Thanks so much for contributing this hook! |
This comment has been minimized.
This comment has been minimized.
this is awesome... |
This comment has been minimized.
This comment has been minimized.
I changed the
|
This comment has been minimized.
This comment has been minimized.
that is nice. |
This comment has been minimized.
This comment has been minimized.
Thank you! |
This comment has been minimized.
This comment has been minimized.
great work! |
This comment has been minimized.
This comment has been minimized.
Thanks, very helpful :) |
This comment has been minimized.
This comment has been minimized.
Very informative, thanks for that! though I'd suggest that you elaborate a bit on what "making the file executable" means, since not everybody knows. :) |
This comment has been minimized.
This comment has been minimized.
@bartoszmajsak I used this as inspiration to create a githook that prepends commit messages with a Jira ticket number (or some other pattern) if it is present in the branch name. I think this may be more practical for a lot of teams than using the entire branch name. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This is awesome. |
This comment has been minimized.
This comment has been minimized.
@Demwunz I haven’t tried it out yet, but you might give this a try. Let us know how it goes |
This comment has been minimized.
This comment has been minimized.
@johncmunson, thanks, we're working with Jira now, so I'll just go ahead and use that! |
This comment has been minimized.
This comment has been minimized.
Thanks for the nice script. Does anyone know how to prevent pepending the commit msg if I do a rebase? |
This comment has been minimized.
Rename
.git/hooks/prepare-commit-msg.sample
toprepare-commit-msg
, paste the script and make the file executable.For instance with branch
ARQ-653
$ git commit -m"Fixed bug"
will result with commit
"[ARQ-653] Fixed bug"
More elaborated way with the reasoning behind this can be found on my old and dusty blog http://blog.bartoszmajsak.com/blog/2012/11/07/lazy-developers-toolbox-number-1-prepend-git-commit-messages/