Git: How to use part of branch name in a commit message

Git logo

So, you’re working on a project where a team norm is to add the story (task) number to all commits associated with it. You perform commits like this:

git commit -m "Some change here [123456]

You also include the story number in the name of the branch. For example:

git checkout-b bashteam/123456

You like to commit frequently. As a result, you think this seems a lot of work, Especially when the story number is in the branch name!

To be a productive developer, you can add a function and a git alias to your bash profile. Don’t worry, I’ll explain!

The end goal

gc -m "something highly productive I'm sure... [$task]"
Command line animation showing usage of the new task command

$task will be replaced by the story number extracted from the name of the branch.

How to do this:

1. Add a function to your bash profile

If you don’t know what a bash profile is, or you don’t know what an alias is, check out my more detailed post called “Command Line Mastery: All About Aliases” where I’ve included a more in-depth explanation about aliases and bash profiles!

(Opens a new tab)
Animation of opening and editing of bash profile file

At the bottom of your bash profile or .alias file, add the following:

function task { task=$(git branch | grep "* bashteam/[0-9].*" | grep -o "[0-9].*")}<!-- /wp:shortcode -->
<!-- wp:shortcode -->alias gc="task; git commit"

2. Put your feet up!

Common Pitfalls

There are a couple of common pitfalls:

  1. Branch name format matters
  2. You need to use double quotes
  3. You need to use the command line

1. Branch name format matters

You need to have a branch name that matches the format from the first pipe to grep, I.e. "\* bashteam/[0-9]*". My team use a branch naming convention of teamname/#123456.

2. You need to use double quotes

You need to write your commit message between double quotes or the string is interpreted literally as [$task].

If you want to experiment with this before committing to using this, you can run the task function to create the $task variable and echo it out.

git checkout -b bashteam/12345 <!-- /wp:shortcode -->
<!-- wp:shortcode -->task <!-- /wp:shortcode -->
<!-- wp:shortcode -->echo "this is a task with story id [$task]";<!-- /wp:shortcode -->
<!-- wp:shortcode -->this is a task with story id [12345]

3. You need to use the command line

I get it, some people prefer to use third-party git tools such as Git Kraken, Source Tree or the one built into their IDE. That’s completely fine! You can be just as productive, or more productive with a tool that you are more familiar with than something you don’t know.

I’m a command line junkie, so I’m at home with this stuff but I get it if you’re not, we can still be friends. If you never use the command line, however, I would encourage you to learn it. Once you get the hang of things, you might find that you can be more productive than before!

How does it work?

The task function

function task { task=$(git branch | grep "* bashteam/[0-9].*" | grep -o "[0-9].*")}

task is the name of the function we are running. When it runs, it creates a variable also called task.

We can refer to task using the dollar symbol as $task.

The function says $task is equal to the result of the chain of commands inside the brackets after the dollar ()

The chain of commands runs git branch, which prints out all of the branches that we have on our machine.

The branch that we have checked out always has asterisks at the start. Our branch name is:

* bashteam/12345 

We pipe (|) this list of branches to a command called grep and tell grep to look for a pattern that matches our branch. The pattern is known as a regular expression.

The regular expression that grep is looking for ("\* bashteam/[0-9]*") reads as follows:

an asterisk * followed by a  followed by bashteam/any amount of numbers from 0 to 9.

When it finds this, it sends that to another grep which looks for the first match of the numbers which is our story number.

We assign this story number to our variable: $task.

The alias

alias gc="task; git commit"

This runs the task command each time you run gcthen it runs the normal git commit command. This means that $task is always set when you are running it.

You can use this as:

gc -m "commit message [$task]"

Conclusion

Did you try this? Did it work for you? Let me know in the comments below, I’d love to hear from you and what you thought!

I’ve also tried out a new plugin in this post to show the code snippets in an editable text box. What do you think? Are they unnecessary / irritating or useful?

2 thoughts on “Git: How to use part of branch name in a commit message

Leave a Reply

Your email address will not be published. Required fields are marked *