Write and maintain custom git commands

Manuel Kunz
StepUp Development
Published in
3 min readJan 15, 2021

Git is a version control system used by many developers all around the globe. It has a lot of functionalities and you might, like me, not always remember everything you can do with it.

Create a remote branch

Let’s dive into it with an example that shows how to create and work with branches.

$ git branch feature/stepup-blog

The branch command will create a local branch only and not push it to the remote. This is pretty straightforward and the syntax is not very hard to remember.
But when I do create a new branch most of the times I want to switch to this branch immediately, so I have to checkout the branch I just created.

$ git checkout feature/stepup-blog

Next you are writing some code and it comes to the point you commit and push your changes. Remember, we are just working locally, so if you want to push your changes, git will tell you the following.

fatal: The current branch feature/stepup-blog has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature/stepup-blog

This basically speaks for itself, your branch has no upstream branch and it even tells you how to set this upstream branch.
Of course this is not the hardest git workflow that exists but it still annoys me after several times. What I like to do is create a little custom git command which execute these steps in one command, so I do not have to do all of these steps each time I create a new branch.

To maintain your git commands I suggest you create your own git repository where you can place your commands in. This enables you to track changes and share your custom commands.

I am using zsh so in my .zshrc file in my home directory, I added the following line to add the path to my repository to the PATH variable.

PATH="$PATH:/Users/mkunz/Private/git-commands"

Now let’s create our first custom git command

We are going to create a command which creates a branch, switches to this branch and pushes the branch to the remote.
Later we want to use this command like any other git command and call it like this.

$ git rbranch feature/stepup-blog

In this case rbranch stands for remote branch but you can name the command whatever you like. So we need to create a file and make it executable. Make sure you are at the root directory of your repository and create the file git-<commandName>. I choose rbranch as the command name so I have to name the file git-rbranch.

$ touch git-rbranch
$ chmod +x git-rbranch

To find out if this works, open the file and add the following lines.

#!/bin/bash
echo "Hello world"

The first line tells Unix that the file is to be executed by /bin/bash. The following command will print Hello World.

Save it and now you can call your git command like the standard git commands you know.

$ git rbranch
// Hello World

This is all you need for your setup.

Add functionality to the command

And now let’s get back to the workflow we had in the beginning and create a new remote branch.
A git command is similar to a shell script so we can pass arguments to it and access these arguments.
So we just take the steps we have and place them in our command, the parameter can be accessed by $1 referring to the first parameter ($0 points to the location of the command).

#!/bin/bashgit branch $1
git checkout $1
git push --set-upstream origin $1

And that is pretty much all you have to do.

I really like to make even simple workflows a little bit simpler. For example a git command that deletes local branches which are no longer available on the remote. Maybe a git command which automatically adds your commit message to your changelog?
I have a repository on GitHub with, at the moment, just a few git commands.
Feel free to contribute to the repository and share your custom git commands that you like to use. Also feel free to reach out to me on Twitter if you have any feedback about this post.

Happy coding.

--

--