Skip to content

Instantly share code, notes, and snippets.

@joaorafaelm
Last active July 13, 2022 21:23
Show Gist options
  • Save joaorafaelm/4fedbd643fba2804df0117ac2b170da7 to your computer and use it in GitHub Desktop.
Save joaorafaelm/4fedbd643fba2804df0117ac2b170da7 to your computer and use it in GitHub Desktop.
Prefix your commits with the branch name
curl -o '.git/hooks/prepare-commit-msg' https://gist.githubusercontent.com/joaorafaelm/4fedbd643fba2804df0117ac2b170da7/raw/prepare-commit-msg && chmod +x .git/hooks/prepare-commit-msg
#!/bin/bash
branchPath=$(git symbolic-ref -q HEAD) #Somthing like refs/heads/myBranchName
branchName=${branchPath##*/} #Get text behind the last / of the branch path
firstLine=$(head -n1 $1)
if [ -z "$firstLine" ] ;then #Check that this is not an amend by checking that the first line is empty
sed -i '.bak' "1s/^/\[$branchName\] /" $1 #Insert branch name at the start of the commit message file
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment