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 a…


This content originally appeared on DEV Community and was authored by Jamund Ferguson

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

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"

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

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?


This content originally appeared on DEV Community and was authored by Jamund Ferguson


Print Share Comment Cite Upload Translate Updates
APA

Jamund Ferguson | Sciencx (2021-04-16T19:04:50+00:00) My first git alias. Retrieved from https://www.scien.cx/2021/04/16/my-first-git-alias/

MLA
" » My first git alias." Jamund Ferguson | Sciencx - Friday April 16, 2021, https://www.scien.cx/2021/04/16/my-first-git-alias/
HARVARD
Jamund Ferguson | Sciencx Friday April 16, 2021 » My first git alias., viewed ,<https://www.scien.cx/2021/04/16/my-first-git-alias/>
VANCOUVER
Jamund Ferguson | Sciencx - » My first git alias. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/16/my-first-git-alias/
CHICAGO
" » My first git alias." Jamund Ferguson | Sciencx - Accessed . https://www.scien.cx/2021/04/16/my-first-git-alias/
IEEE
" » My first git alias." Jamund Ferguson | Sciencx [Online]. Available: https://www.scien.cx/2021/04/16/my-first-git-alias/. [Accessed: ]
rf:citation
» My first git alias | Jamund Ferguson | Sciencx | https://www.scien.cx/2021/04/16/my-first-git-alias/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.