oh-my-zsh git plugin tip

Just a handy little tip.

The zsh git plugin offers a handy little function called ‘current_branch’. This will return the current git branch in the current git repository.

You can call zsh functions within commands by wrapping them with ‘$()’.

This helps me to reduce one of the little frustrations I have with git – specifying the branch name when pushing.

Now, I can just type this:

$ git push origin $(current_branch)

Of course, I’ve shortened this by creating an alias for current_branch called ‘cb’, so I just type:

$ git push origin $(cb)

The alias can be added to your .zshrc with the following code:

alias cb="current_branch"

Simple and helpful.

recent-branches – a quick git command

I’m often switching between the same few branches in git – whether I’ve been distracted by doing a quick fix in another branch, or am doing a code review on a colleague’s branch – and get frustrated with having to type out the name of said branches all the time.

With that in mind, I created a quick ruby script that will show me my most recently checked-out branches (up to a limit of my choosing) and allow me to check one out quickly by typing a number.

As mentioned, the limit can be set by the user, simply by adding the key ‘recent.limit’ to the git config (either –global or in the current project) with an appropriate number.

If this sounds useful, grab the code here: https://github.com/ascii-soup/git-recent-branches