Skip to content

Instantly share code, notes, and snippets.

@jasonk
Last active April 27, 2019 16:08
Show Gist options
  • Save jasonk/c29679fa77f4c81d20a31608795ab265 to your computer and use it in GitHub Desktop.
Save jasonk/c29679fa77f4c81d20a31608795ab265 to your computer and use it in GitHub Desktop.
Lazy developers use branch names to keep track of their tickets

On my team we like to put ticket numbers in commit messages, so that Jira finds them and puts links to the commits in the ticket. The problem is that I have a really hard time remembering my ticket numbers. What I've been doing is creating a branch for each ticket, but while that helps me remember the numbers, it still doesn't help me remember which ticket is which. I had tried posting comments to myself in Slack to keep track of them, but then it occurred to me that I didn't need to be using just the ticket number as the branch name, so now I'm trying something new.

Instead of creating branches with just the ticket number, I'm using ticket/short-description. The length of the branch name doesn't affect much, thanks to tab completion, and having that information available at a glance right in the repo is huge, especially since I have the branch name as part of my prompt.

[me@my-machine:dir PROJ-1234/fix-ci ]$ git branch
  PROJ-1234/fix-ci
  PROJ-1233/remove-stupid-widgets
  PROJ-1232/fix-broken-widgets
  PROJ-1231/add-awesome-widgets

Could I get any lazier? You bet I can! Drop this into your repo in .git/hooks/commit-msg (make sure it's executable):

#!/bin/bash

# Change this to match the format that your tickets are in
TICKET_RE="PROJ-[0-9]+"

FILE="$1"
BRANCH="$(git symbolic-ref --short HEAD)"
BRANCH="${BRANCH%%/*}"

# If the branch name looks like a ticket number, then we make sure the
# message starts with the ticket number
if [[ $BRANCH =~ ^${TICKET_RE}$ ]]; then
  MSG="$(head -1 "$FILE")"
  if ! [[ $MSG =~ ${BRANCH}* ]]; then
    sed -i.bak -e "1s/^/$BRANCH - /" "$FILE"
  fi
fi

Now whenver I commit from a branch that has a ticket-number name but forget to start my commit message with the ticket number, git will add it for me.

@milind-shakya-sp
Copy link

nice! this pre-commit hook does pretty much the same thing if you use pre-commit. https://github.com/milin/giticket#features

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment