5 min read
Automating Git Commands

Introduction

About 6 months ago, courtesy of the ALX Software Engineering Program, I wrote my first Bash script. About the same time, I also made my first git commit using the command-line interface. I find it very amusing that it took me 800+ commits using git add ., git commit -m "Commit Message", and git push before I realized that I could simplify the process by combining all three commands into one. And I am guessing I am not the only one. Well, this is how I managed to do it. But first, why?

Why Automate Git Commands?

Automating Git commands is a great way to streamline your development workflow. Instead of typing the same series of Git commands every time you make changes to your project, you can simplify it to a single command. This not only saves time but also reduces the risk of making mistakes. I mean, how many times have you written git puhs, got an error message, and then typed again?

Creating the Shell Script

To get started, you’ll need to create a shell script that combines git add ., git commit, and git push into one command. Here’s how to do it:

Open your terminal

Open your favorite terminal application, and navigate to your project directory.

Create a new shell script

Use your favorite text editor to create a new file. You can name it something like push.sh.

You need to know the file’s location for future use. To keep my workspace neet, I created a hidden folder called bash_assets in my root folder. But you can create it however you want, just take note of the location.

me@tapiwamla:~$ mkdir .bash_assets
me@tapiwamla:~$ cd ./bash_assets
me@tapiwamla:~/.bash_assets$

In that hidden folder, I then created the script. My favorite editor is vi, so I used:

me@tapiwamla:~/.bash_assets$ vi push.sh

Fun fact: Vim is an improved clone of the vi text editor. Running vi where Vim is installed essentially opens Vim.

Write the script

In the push.sh file, add the following lines:

#!/bin/bash

if [ $# -eq 0 ]; then
  echo "Usage: push 'commit message'"
  exit 1
fi

message="$1"

git add .
git commit -m "$message"
git push

This script checks if you provide a commit message as an argument and then executes the Git commands accordingly.

Save and exit

Save the file and exit the text editor. In vim, you can do this by typing :wq to save and exit.

Make the script executable

You need to make the script executable by running the following command:

me@tapiwamla:~/.bash_assets$ chmod +x push.sh

Creating an Alias

With your shell script ready, you can create an alias that allows you to run it with a simple command anywhere without needing to specify the script’s path each time. Here’s how to set up an alias in your shell’s configuration:

Edit your shell’s configuration file

You can edit your shell’s configuration file, typically ~/.bashrc, and ~/.zshrc for Zsh. However, following the recommendation in the ./bashrc file itself, you may want to put all your aliases into a separate file like ./bash_aliases instead of adding them directly.

You can use vior another text editor for this:

vi ~/.bash_aliases

Create an alias

Add the following line to the file. Replace ~/path/to/your/push.sh with the actual path to your push.sh script:

alias push='sh ~/path/to/your/push.sh'

If you created a setup similar to mine, then you run:

alias push='sh ~/.bash_assets/push.sh'

Save and reload

Save the configuration file and exit the text editor. Then, reload your shell configuration to apply the changes:

source ~/.bashrc

Using Your New Git Shortcut

Now that you’ve set up the alias, you can use it to automate Git commands with a single, convenient command. To add, commit, and push your changes, simply run:

push "Your Commit Message"

This will execute your push.sh script, and your changes will be added, committed, and pushed with the provided commit message.

Conclusion

Automating repetitive tasks in your development workflow can save you time and reduce the risk of errors. Creating a Git shortcut, as described in this posts post, is a simple but effective way to streamline your Git processes. Whether you’re a seasoned developer or just getting started, this technique can help you work more efficiently and maintain a clean and well-organized Git history. You are welcome!