Custom Aliases

How to create custom git aliases, or any other alias, for that matter.

If using the same git commands over and over again is getting tedious, you can create aliases for them in your .zshrc file. (or .bashrc if you're using bash)

An alias is a way to run a command or a series of commands using a shorter name. For example, if you want to run git status every time you want to see what's going on with your repo, you can create an alias called gst that will run git status for you.

Recently, I decided to install Oh My Zsh but only for their git plugin which comes packed with a bunch of useful aliases.

But, there are some aliases that I wanted to create myself, and this is an elaboration of one.

For my use case, I wanted to create an alias that would run git switch main, git pull, git branch -D dev, and git checkout -b dev all at once. Let's call this alias git-refresh-branch.

Why?

Well, I have a workflow where I create a new branch called dev or develop from main every time I start working on a new feature. Because more often than not, I am already on the dev branch, I have to switch to main, pull the latest changes, delete the dev branch, and then create a new dev branch from main. Like a curious engineer, I wanted to automate this process. :)

I use warp; a modern, Rust-based terminal which is very fast and has a lot of cool features. I highly recommend it.

So, first things first, let's hop into our terminal and open up our .zshrc file.

		code ~/.zshrc
	

Then, we'll add our alias to the bottom of the file.

		# custom git aliases
alias git-refresh-branch='git switch main && git pull && git branch -D dev && git checkout -b dev'
	

💡 To get Warp's autocomplete to work with your aliases, you might want to switch to a new terminal tab by just pressing cmd + t or clicking the plus icon in the top right corner of the terminal tab. A nice feature is that your current working directory will be the same in the new tab, unless you overwrote the default behaviour.

Now, we can run git-refresh-branch, and it will run all the commands we specified in the alias.

		git-refresh-branch
	

Now, this is all nice and well, but say we wanted to make this alias a bit more dynamic. What if we wanted to be able to pass in the branch names as arguments to the alias? We can easily do this by modifying our alias to invoke a function instead of running the commands directly.

To do this, let's remove our old alias and replace it with a function.

		# git aliases
git-refresh-branch() {
  if [ -z "$1" ] || [ -z "$2" ]; then
    echo "Usage: git-refresh-branch <main_branch> <dev_branch>"
    return 1
  fi

  git switch "$1" && git pull && git branch -D "$2" && git checkout -b "$2"
}
alias grfrsh=git-refresh-branch
	

This block of code is a check within the git-refresh-branch function to ensure that the function is called with the correct number of arguments. Let's break it down:

  • if [ -z "$1" ] || [ -z "$2" ]; then - This is a check to see if the first or second argument is empty. If either of them are empty, then the function will return an error.
  • echo "Usage: git-refresh-branch <main_branch> <dev_branch>" - This is the error message that will be printed if the function is called with the wrong number of arguments.
  • return 1 - This is the exit code that will be returned if the function is called with the wrong number of arguments. This is important because it will tell the terminal that the function failed to run.

Now, we can run grfrsh with the correct number of arguments, and it will run the commands we specified in the function.

		grfrsh main dev
	

That's it!