Juha-Matti Santala
Community Builder. Dreamer. Adventurer.

Command line magic with git and bash history

So, in the beginning of January I started as an engineering intern in Chartio and since that my workflow has really improved a lot and I’ve learned and discovered some tricks.

First one I want to share, made a huge difference for me:

So, whenever I had previously needed to run a bash command again, I would do history | grep [keyword] and then look at the number of the command I want to rerun and run ![number] to rerun the command. And I was quite happy, it was efficient enough for me not to try and google for some better way.

Until last week, when my coworker showed me that using CTLR+r on bash prompt (using emacs mode), bash enters a reverse-search mode where you can type a part of the command and bash goes through the history and suggests starting from latest command a matching one. Pressing CTLR+r again, it moves one step further in history. You can run the command or modify it on the fly. So instead of two commands, you can just hit CTLR+r and ENTER to run the command. Awesome.

Another thing I discovered has to do with git. If you don’t yet know or use git, start now. There’s no reason for you to not use it. Anyway, my git usage has gone through the roof now that I started working here. So, when using branch names like bugfix-1234, issue-1950or so, it does get bit hard to remember what those are all about. And I’m very visual guy so I have post-its all around my desktop and display to keep me on track of things.

So I wrote some bash functions and git aliases to make things more convenient for me. In my ~/.bash_profile, I have following:


function parse_git_branch_name {
  git rev-parse --abbrev-ref HEAD
}

function parse_git_description {
  git config branch.$(parse_git_branch_name).description
}

alias gs='echo $(parse_git_branch_name): $(parse_git_description) && git status'

First function gets the name of current branch and second on gets description saved to the branch defined by git branch --edit-description. For easier editing, I also have a git alias in ~/.gitconfig for editing description:


[alias]

edit = branch --edit-description

So everytime I want to add or edit a branch description, I do git edit and my habit is to do gs all the time so then I see the description followed by status of my git repo. Awesome.