TWIL
git
Featured image for TWIL blog post on Git aliases, showing a simplified illustration of branch management and code snippets.

Welcome to TWIL, your weekly source for software development tips and tricks. In this installment, Katie shares some of her favorite Git aliases, teaching us how to streamline our workflow with powerful shortcuts for branch searching and recent branch tracking. Her examples are efficient techniques to keep your repository exploration both nimble and insightful. Join us in embracing these time-saving commands and hone your Git prowess with every commit!

Git aliases: Branch Search and Recent Branches

Search a branch by name by piping the output of git branch to grep:

# get the branches that match the given pattern
branch-search = !git branch | grep --color=always
brs = branch-search

Usage example:

$ git brs bug
* bug -branch
  bug/another-bug 
  bug/fix-thing-a 
  bug/search-for-bug-branches-example

recent-branches (or brecent)

Show the most recent branches (number determined by --count flag, set to 7 here), sorted by most recent commit.

# get the most recently-committed branches
recent-branches = "!git for-each-ref --sort=-committerdate refs/heads --count=7 --color=always --format='%(color:black)%(committerdate:relative)|%(HEAD)%(color:green)%(refname:short)|%(color:white)%(subject)%(color:reset)' | column -ts'|'"
brecent = recent-branches

Usage example:

$ git brecent
75 seconds ago         *bug-branch    Most recent commit
24 hours ago           master         Add nice new git branch search and sort aliases
2 weeks ago            test2          Commit #2
1 year, 1 month ago    git-fns        WIP
1 year, 1 month ago    test           WIP
1 year, 3 months ago   work           WIP

The --format flag can also be adjusted to display the output as desired. As it is, it will output:

  • The time since the most recent commit (black) : %(color:black)%(committerdate:relative)
  • Asterisk indicating current branch (black) : %(HEAD) *
  • The branch name (green) : %(color:green)%(refname:short)
  • The message of the most recent commit (white) : %(color:white)%(subject)

Each section is separated with a |, and the output is then piped into column -ts'|' for column alignment where the | characters are.

*This is black because it follows the previous %(color:black) and precedes the following %(color:green), but it appears ahead of the branch name because it is placed after the | separating the commit date output and the branch name output

Resources

  • git
Katie Linero's profile picture
Katie Linero

Senior Software Engineer

Related Posts

Dynamic team of software developers from Cuttlesoft, highlighting their organizational maturity enabled by their re-branded image.
August 18, 2022 • Frank Valcarcel

The New Cuttlesoft

To reimagine Cuttlesoft’s brand, we partnered with the experts at Focus Lab. With their guidance, we identified the ways Cuttlesoft was failing to meet its full potential.

A guide to geofences with mobile applications
August 26, 2020 • Marisa Gomez

A Guide to Geofencing in Mobile Applications

Geofencing is a tool used to monitor a location and the region that surrounds it – through events like entering, exiting, or staying within the region. They are registered on a device and will be monitored whether an application is open or closed.