DEV Community

Srebalaji Thirumalai
Srebalaji Thirumalai

Posted on • Originally published at gitbetter.substack.com on

Automate repetitive tasks with custom git commands

This post was originally posted in the newsletter GitBetter. If you are interested in leveling up your game in Git, you can subscribe to it.

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 mycommandin 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-lazypushin the above created directory. And add the following code. Refer to the code in the gist here.

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“

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.

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.

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.

git-hard-delete

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

You can find the code in the gist here.

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 to get tricks, tips, and advanced topics of Git.

Thank you for reading :)

Top comments (0)