Skip to Main Content
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

Digital pioneer journeying through technological quirks, reminiscent of vast waterfalls and rocky terrains
April 5, 2017 • Kyle Misencik

Infinitely dispatching actions + React, Redux & React Router v4

While implementing JWT for an application, we ran into a quirk with React Router v4. Here, we hope to show how to implement protected client-side routes while avoiding some of the pitfalls we encountered.

Turtle swimming close to a life preserver as a metaphor for developers using Git version management to stay afloat
May 4, 2017 • Nick Farrell

Life-Saving Git Tips

From salvaging lost commits to streamlining your workflow, discover how to make your coding life smoother and avoid common pitfalls.