Automate repetitive tasks with custom git commands
Tags: git, programming, beginners, tutorial
Published: 2020-05-14 06:53:15

---
title: Automate repetitive tasks with custom git commands
published: true
description: How to write custom git commands to automate repetitive tasks
tags: git, programming, beginners, tutorial
canonical_url: https://gitbetter.substack.com/p/automate-repetitive-tasks-with-custom
---
### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.

[![](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F052874b8-f7b7-44e1-89d7-6379351cdb55_600x849.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F052874b8-f7b7-44e1-89d7-6379351cdb55_600x849.png)

Git helps us to write our own custom commands. With these commands, you can write your own git tasks. These git commands can be written in any language you prefer like Ruby, JavaScript.

In this tutorial, I will explain how to write your own custom commands and I will give some examples of it. We will be using bash for this tutorial.

Writing custom git commands are simple as these three steps:

1. Create a file with a name in the given format `git-mycommand. `And this file should be given executable access.

2. The created file should be included in `$PATH. `This can be done in `bashrc` or in `zshrc`

3. Then you can run the command `git mycommand `in any of the git repo you have.

### Writing our first custom command

Let’s write a custom git command to add, commit, and then push the changes to remote with one command. This is just an example to write a custom command so don’t worry about the use-case of the command we are gonna build.

#### 1. Creating a file for our command

It’s good to have a directory to contain all our custom commands. So that it will be organized. Let’s create a directory.

```
mkdir my-git-custom-commands
```

Create a file named `git-lazypush `in the above created directory. And add the following code. Refer to the code in the [gist here](https://gist.github.com/srebalaji/4b23fb62f6ce26c17b73ca07ae7cfc67#file-git-lazypush).

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F86a0a710-e9cf-448f-898d-41768b3e4703_1700x858.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F86a0a710-e9cf-448f-898d-41768b3e4703_1700x858.png)

Last but not least, make that file as executable

```
chmod +x git-lazypush
```

#### 2. Add custom commands to $PATH

This is quite important. This will help Git to recognize the custom commands.

We are adding the directory to $PATH. Add the following line to `bashrc` or to `zshrc`

```
export PATH=$PATH:/your-directory/my-git-custom-commands
```

You can now source the file or start a new instance of the terminal

```
source ~/.zshrc
```

#### 3. Running our custom command

With everything in place, it’s time to run our custom command. Go to any of your local git repo and run the following command

```
git lazypush “Your commit message“
```

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F310c1b99-e7fb-4795-9123-1294d724e712_809x376.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F310c1b99-e7fb-4795-9123-1294d724e712_809x376.png)

As you can see, I have created a new file and have executed the custom command.

And it is evident that files are added, committed, and pushed to the remote server.

Git commands are very useful and can be used to automate the usual tasks in your workflow. So try to find tasks you find repetitive and automate it.

### Ideas for custom commands

I will add some ideas of custom commands that I can think of.

#### git-switch-to

The custom command to checkout to a new branch with all the unstaged changes present in the current branch.

You can find the code in the **[gist here](https://gist.github.com/srebalaji/4b23fb62f6ce26c17b73ca07ae7cfc67#file-git-switch-to)**.

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F73e8217b-8984-47d2-8bd1-f565b1734b55_956x884.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F73e8217b-8984-47d2-8bd1-f565b1734b55_956x884.png)

#### git-remote-diff

The custom command to view the difference between the local and the remote of the specified branch.

You can find the code in the **[gist here](https://gist.github.com/srebalaji/4b23fb62f6ce26c17b73ca07ae7cfc67#file-git-remote-diff)**.

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F51616e0f-473e-4ff3-bd31-accaff15c50e_1342x842.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F51616e0f-473e-4ff3-bd31-accaff15c50e_1342x842.png)

#### git-remote-log

The custom command helps you to view the commit logs between the local and remote changes of the given branch.

You can find the code in the **[gist here](https://gist.github.com/srebalaji/4b23fb62f6ce26c17b73ca07ae7cfc67#file-git-remote-log)**.

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2dfd68be-21e7-4c83-891a-5606b6a578a8_1342x842.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2dfd68be-21e7-4c83-891a-5606b6a578a8_1342x842.png)

#### git-hard-delete

The custom command to delete a branch both locally and remotely.

You can find the code in the **[gist here](https://gist.github.com/srebalaji/4b23fb62f6ce26c17b73ca07ae7cfc67#file-git-hard-delete)**.

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F88546bff-eb13-4796-889a-42579a96120e_956x842.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F88546bff-eb13-4796-889a-42579a96120e_956x842.png)

#### git-lazypr

The custom command to create a pull request. There are APIs available in Github, Gitlab to create a PR in a repo. You can use that to create a PR from your command line itself.

If you have any queries, you can just leave a comment. :)

#### *If you have come this much, then I think you are much interested in Git. You can subscribe to [GitBetter](https://gitbetter.substack.com/) to get tricks, tips, and advanced topics of Git.*

Thank you for reading :)