DEV Community

Jamund Ferguson
Jamund Ferguson

Posted on

My first git alias

At work I'm often jumping back and forth between various branches and pull requests and I sometimes find myself struggling to remember the name of a recent branch I need to use.

For years I've used git reflog as a short-cut to look at my git history and identify my recent work. It works okay, but it's very hard to make sense of:

git reflog output

Other times I try to use git branch, but that doesn't give you much context about anything and of course it can be easy to drown in the large number of branches:

~/d/n/webapp (typescript ✔) git branch
  add-cognito-auth
  doritos
  eslint-prettier
  main
* typescript
  updated-husky
Enter fullscreen mode Exit fullscreen mode

Recently I got frustrated by this and decided to find a solution and stumbled on this stackoverflow question, How can I get a list of git branches ordered by most recent commit?, which taught me two really useful techniques.

  1. You can sort branches by commit date using git branch --sort=-committerdate which is great in an of itself (watch out for the - after sort= which puts the most recent branches first)
  2. You can enhance this output a bit using this wild syntax git branch --sort=-committerdate --format='%(HEAD)%(color:yellow)%(refname:short) | %(color:bold green)%(committerdate:relative) | %(color:blue)%(subject)%(color:reset)' --color=always which I'm never going to remember.

Which led me to create my first git alias after over 12 years of using it. Here's how I did it and here's what it looks like:

git config --global alias.branches "branch --sort=-committerdate --format='%(HEAD)%(color:yellow)%(refname:short) | %(color:bold green)%(committerdate:relative) | %(color:blue)%(subject)%(color:reset)' --color=always"
Enter fullscreen mode Exit fullscreen mode

That command added this to the boottom of my ~/.gitconfig file:

[alias]
        branches = branch --sort=-committerdate --format='%(HEAD)%(color:yellow)
%(refname:short) | %(color:bold green)%(committerdate:relative) | %(color:blue)%(subject)%(color:reset)' --color=always
Enter fullscreen mode Exit fullscreen mode

Now when I type git branches I see this lovely output:
nicely formatted git branch list

Git isn't always the easiest to use, but this alias has made it just a little bit better, for me anyway. What kind of aliases do you use?

Top comments (5)

Collapse
 
franaguiar profile image
Francisco Aguiar

Looks pretty good, I will add it to my .gitconfig

I have several for repetitive small commands, for example, I have one to avoid type git push origin branchName, I just type git ps, and the same for pull with git pl

Another one to fetch origin and prune, git fp.

I liked yours, thanks

Collapse
 
jessekphillips profile image
Jesse Phillips

This does look like a good one. I also have one alias which I created somewhat on the recent side.

git commit --fixup -> git fixup

It is a small change but it is nice.

Collapse
 
tyler36 profile image
tyler36

Sometimes I just wanna reset everything and start fresh

## Hard reset git - !DANGER
alias nah='git reset --hard && git clean -df'
Enter fullscreen mode Exit fullscreen mode
Collapse
 
edm00se profile image
Eric McCormick

Nice! I'm pretty sure I'll be using this with some frequency.

Collapse
 
reshalem profile image
M. Ali Setia Pratama

Great and useful trick! Question, can I change the color with hex value?