Git alias with parameters
Tags: bash, git, gitalias
Published: 2020-04-04 17:34:23

---
title: Git alias with parameters
published: true
date: 2020-04-04 18:52:00 UTC
tags: bash,git,git-alias
canonical_url: https://le0nidas.gr/2020/03/27/git-alias-with-parameters/
cover_image: https://unsplash.com/photos/H7LxvEmVZnE
---

In the company I work we use the [gitflow](https://nvie.com/posts/a-successful-git-branching-model/) [workflow](https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow) and in every feature branch we make sure that part of its name is the task’s id which includes a unique number. So what we end up with is something like:

> feature/T12345-make-the-app-rock

What I wanted was an easy way to checkout a branch using just the number. I wanted (spoiler: and succeeded) something like this:

> git ch 12345

#### Terminal

Lets see how we can checkout a branch without an alias but by using the task’s number.

##### Get the branch

We can get the branch by listing all branches (`git branch)` and then `grep` for the one that contains the task’s number:

> git branch | grep 12345

this will return _feature/T12345-make-the-app-rock_

##### Checkout to it

Having a way to get the branch makes the checkout quite easy since we can use [bash’s command substitution](https://wiki.bash-hackers.org/syntax/expansion/cmdsubst) to execute the above commands and use the result directly in the checkout:

> git checkout $(git branch | grep 12345)

##### Put it in a function

We are now able to checkout the task’s branch but it would be nice if we could change the task’s number more easily. Lets create a function and pass the number as a parameter:

{% gist https://gist.github.com/le0nidas/ffc0705d4b131b7e3f362cf09f72928b %}

- The `$1` is where the parameter will be used (if we had more parameters we would use `$2` for the second, `$3` for the third etc).
- You can check the function by writing it directly in the terminal. Just make sure that you add newlines (press enter):

![](https://le0nidasgr.files.wordpress.com/2020/03/screenshot-from-2020-03-28-20-50-06.png?w=279)

#### The alias

We can change branches easily but once we close the current terminal session we lose everything. Our goal is to have all that in a [git alias](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases).

Creating an alias can be achieved either by using `git config`

> git config –global alias.ch checkout

or by doing it _manually_ and adding the alias directly into the `.gitconfig` file (usually located in the home directory). The file should end up with something like:

{% gist https://gist.github.com/le0nidas/fbacadefc2594c82f7524a25a88683a3 %}

Having just the alias will not help us much. As is, it will work only if we give it the entire branch:

> git ch feature/T12345-make-the-app-rock

What we need is a way to connect the function that we created with the alias.

The answer is provided by git itself since it has a way to create aliases that do not execute a git command but an external command:

> However, maybe you want to run an external command, rather than a Git subcommand. In that case, you start the command with a `!` character. This is useful if you write your own tools that work with a Git repository.
> 
> <cite><a href="https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases">git docs</a></cite>

Following the docs we can change the gitconfig file to look like this (this time the change must be made only manually because our function will need to have newlines):

{% gist https://gist.github.com/le0nidas/4eec7518eaeb9ad0f7a280f6840c1e5c
%}

What will happen when we write `git ch 12345` is:

1. `git ch` will be replaced with everything inside the `"..."` (minus the `!`) ending up with two commands
2. the first one creates the `f` function and
3. the second one calls f with the parameter 12345: `f 12345`

And that’s it! We now have a git alias that will allow us to checkout a branch using part of its name.

###### feature image by [Zach Reiner](https://unsplash.com/@_zachreiner_) @ [unsplash](https://unsplash.com/photos/H7LxvEmVZnE)