A Git hook that automatically adds Linear ticket IDs and descriptions to your commit messages based on branch names.
This was built in an afternoon at Super Happy Dev House with heavy use of Claude.
Slides: https://jaredsohn.github.io/git-linear-hook/slides.html
Motivation was as a follow-up to this Hacker News thread for the git-smart-squash project.
- Extracts Linear ticket IDs from branch names (e.g.,
ABC-601,DEV-123) - Adds descriptive context from branch names to commit messages
- Smart text processing - removes prefixes, cleans up separators, capitalizes appropriately
- zsh optimized - uses native zsh features for better performance
- Works with all commit types -
git commit,git commit -m,git commit -a -m - No API dependencies - works offline using only branch name information
Branch: feature/abc-601-user-authentication-system
Your commit: add password validation
Result:
[ABC-601] User authentication system
add password validation
Branch: bugfix/dev-456-fix-null-pointer-exception
Your commit: add null check
Result:
[DEV-456] Fix null pointer exception
add null check
-
Download the hook:
curl -o .git/hooks/prepare-commit-msg https://raw.githubusercontent.com/jaredsohn/git-linear-hook/main/prepare-commit-msg
-
Make it executable:
chmod +x .git/hooks/prepare-commit-msg
- Copy the script to
.git/hooks/prepare-commit-msgin your repository - Make it executable:
chmod +x .git/hooks/prepare-commit-msg
To use this hook across all your repositories:
-
Create a global hooks directory:
mkdir -p ~/.git-hooks -
Copy the hook:
cp prepare-commit-msg ~/.git-hooks/ chmod +x ~/.git-hooks/prepare-commit-msg
-
Configure Git to use global hooks:
git config --global core.hooksPath ~/.git-hooks
The hook automatically handles common prefixes:
feature/abc-601-description→[ABC-601] Descriptionfeat/dev-123-bug-fix→[DEV-123] Bug fixbugfix/xyz-456-urgent-patch→[XYZ-456] Urgent patchhotfix/lin-789-security-fix→[LIN-789] Security fixchore/team-111-update-deps→[TEAM-111] Update deps
The hook recognizes Linear ticket IDs with the pattern:
- 2+ letters (uppercase or lowercase)
- Dash (
-) - 1+ numbers
Examples: ABC-601, dev-123, TEAM-4567
- Extracts the Linear ticket ID from your current branch name
- Removes common prefixes (
feature/,bugfix/, etc.) - Cleans up the description by removing the ticket ID and separators
- Formats the text by converting dashes/underscores to spaces
- Capitalizes appropriately (first word only)
- Prepends to your commit message with clean formatting
The hook automatically skips processing for merge commits and amends. If you want to skip additional branches, you can modify the script or checkout those branches without the hook.
You can modify the script to change the output format. Look for this line:
if [ -n "$DESCRIPTION" ]; then
PREFIX="[$TICKET_ID] $DESCRIPTION"
else
PREFIX="[$TICKET_ID]"
fi- zsh (the hook is optimized for zsh)
- Git (any recent version)
- grep (standard on Unix/Linux/macOS)
-
Check permissions:
ls -la .git/hooks/prepare-commit-msg # Should show: -rwxr-xr-x (executable) -
Verify the shebang:
head -1 .git/hooks/prepare-commit-msg # Should show: #!/bin/zsh
The hook will silently exit if:
- Branch name doesn't contain a Linear ticket ID pattern
- You're on a merge commit or amend
- Ticket ID is already in the commit message
Test the ticket ID extraction manually:
# Check what ticket ID would be extracted
BRANCH_NAME=$(git symbolic-ref --short HEAD)
echo "Branch: $BRANCH_NAME"
echo "Ticket ID: $(echo "$BRANCH_NAME" | grep -oE '[A-Za-z]{2,}-[0-9]+' | head -n1)"- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Linear - The issue tracking tool this hook is designed for
- Git Hooks Documentation
- Linear GitHub Integration - Official Linear + Git integration
- Initial release
- Support for Linear ticket ID extraction
- Smart branch name description parsing
- zsh optimization
- Clean commit message formatting