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
branch-search (or brs)
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
- this StackOverflow question with multiple approaches for listing recent branches
- git