Automating a Basic Git Flow with BASH Scripts

This post will teach you how to create bash scripts that make the using of the basic git commands more efficient.

In my everyday job, 95% of the git commands I run follow the exact same pattern: I checkout a new feature branch starting from a development branch, I commit all my changes to it, and then push it to origin.

But because I do this all the time, I waste a lot of time writing the full commands and making sure that the Jira task I am developing for is associated with the commits and branch, meaning I have to write the full task identifier (in my case something like SREAPP-4360) for each commit and when checking out/pushing the branch.

Given this, we will create four scripts for the following git commands, using shorthand names for each of them, with the following behavior:

  • Git Add: a simple alias for git add, called ga, so I don’t have to write all of it everytime
  • Git Push: aliased gp, pushes the current branch to origin, creating it automatically if it doesn’t exist
  • Git Commit: aliased gcm, receives a task number and a message and commits the current changes, appending the project alias to the task number
  • Git Checkout: aliased gch, runs git fetch, and checkouts a branch with the given name, creating it if it doesn’t exist

Creating the files

To start we will simply run a touch command to create the four files that will contain ours scripts. For this, I created a folder called Scripts in my home directory

mkdir ~/Scripts
touch ga.sh gp.sh gch.sh gcm.sh

Writing the scripts

After we have our files, we will paste the contents below in each of them using any text editor, like nano.

Git Add

#!/bin/bash

# Check if at least one file or pattern is provided
if [ $# -eq 0 ]; then
  echo "Error: No files specified."
  echo "Usage: ga <file-patterns>"
  exit 1
fi

# Perform git add with the provided arguments
git add "$@"

Git Push

# Get the current branch name
current_branch=$(git symbolic-ref --short HEAD)

# Check if the current branch exists on origin
branch_exists_on_origin=$(git ls-remote --heads origin $current_branch)

if [ -z "$branch_exists_on_origin" ]; then
  echo "Branch does not exist on origin. Pushing with upstream."
  git push -u origin $current_branch
else
  echo "Branch exists on origin. Pushing normally."
  git push
fi

Git commit

# Check if the correct number of arguments is provided
if [ $# -ne 2 ]; then
  echo "Error: Two arguments required."
  echo "Usage: gcm <message> <tasknumber>"
  exit 1
fi

# Extract the arguments
commit_message=$1
task_number=$2

# Construct the full commit message
full_commit_message="SREAPP-$task_number $commit_message"

# Perform git commit with the constructed message
git commit -m "$full_commit_message"

Git checkout

# Check if branch name is provided
if [ -z "$1" ]; then
  echo "Error: No branch name provided."
  echo "Usage: gco <branch-name>"
  exit 1
fi

branch_name=$1

# Fetch the latest branches from the remote
git fetch

# Check if the branch exists either locally or remotely
branch_exists=$(git branch --all | grep -w "$branch_name")

if [ -z "$branch_exists" ]; then
  echo "Branch does not exist. Creating and checking out new branch '$branch_name'."
  git checkout -b "$branch_name"
else
  echo "Branch exists. Checking out branch '$branch_name'."
  git checkout "$branch_name"
fi

Making files executable

Once we have our files with the scripts in them, we have to make them executable by changing their permissions. We will run chmod to make them executable

chmod +x ga.sh gp.sh gch.sh gcm.sh 

After this, we will have to tell our command line that the Scripts folder that we created in the first step contains scripts that we would like to run from anywhere on our terminal, without having to write their full path. For this, we will create symbolic links from the Scripts folder to the folder where our system keeps executables

sudo ln -s ~/Scripts/gcm.sh /usr/local/bin/gcm
sudo ln -s ~/Scripts/ga.sh /usr/local/bin/ga
sudo ln -s ~/Scripts/gp.sh /usr/local/bin/gp
sudo ln -s ~/Scripts/gch.sh /usr/local/bin/gch

Finally, we will run the source command to reload our terminal session and have these scripts available

source ~/.zshrc

After following all these steps, our four new commands will be ready to be used from anywhere in our terminal.

Leave a comment