These two Bash scripts are guaranteed to simplify your Git workflow.

Introduction:

Git is a powerful version control system that helps you track changes to your code. However, the basic Git commands can be tedious to type over and over again. This blog post will show you two Bash scripts that can simplify your Git workflow.

Script 1: Simplifying the Add, Commit, and Push Process

The first script simplifies the process of adding, committing, and pushing changes to a remote Git repository. This means you can perform all three actions - add, commit, and push - to the remote repository simply by running a single command. To use the script, first, you need to save the code as a shell script in your git repository as the name given below (git-workflow. sh) or anything you want, and then simply run the following command:

./git-workflow.sh "comment"

The script will first add all of the changes in your working directory to the staging area. It will then commit the changes to the current branch, and finally push the changes to the remote repository.

This is the code of the first script:

#!/bin/bash

# Check if there is at least one argument provided (the commit message)
if [ $# -eq 0 ]; then
    echo "Please provide a commit message."
    exit 1
fi

# Stage all changes
git add .

# Commit with the provided message
git commit -m "$1"

# Push the changes to the remote repository
git push

Script 2: Automating the Fetch and Pull Process

The second script automates the process of fetching and pulling changes from a remote Git repository whenever you open Git Bash. To use the script, simply add the following line to your .bashrc file:

💡
The . bashrc file location is in the user's home directory (C:\Users\YOUR-USERNAME), and any text editor can read and edit its contents.
source ~/git-workflow.sh

Now, whenever you open Git Bash, the script will automatically fetch and pull changes from the remote repository. This will help you keep your local repository up-to-date effortlessly.

Below is the code for the second script:

# Auto-fetch and pull Git repository on Git Bash startup
function git_auto_fetch_pull() {
branch=$(git rev-parse --abbrev-ref HEAD)
remote="origin"

echo "Fetching latest changes from $remote..."
git fetch $remote > /dev/null 2>&1

if [ $(git rev-list HEAD...$remote/$branch --count) -gt 0 ]; then
    echo "Pulling changes from $remote/$branch..."
    git pull $remote $branch
else
    echo "Your repository is up to date."
fi
}

# Check if the current directory is a Git repository and auto-fetch/pull
function git_auto_fetch_pull_on_startup() {
if [ -d ".git" ]; then
    echo "Welcome to your Git repository!"
    git_auto_fetch_pull
    echo "You are all set!"
else
    echo "You are not inside a Git repository."
fi
}

# Call the function on startup
git_auto_fetch_pull_on_startup

Conclusion:

These two Bash scripts can help you simplify your Git workflow and save you time. If you are a regular user of Git, I encourage you to give these scripts a try.

Please make sure to visit the GitHub repository for the bash scripts and you'll find detailed information about the scripts.

Finally, make sure to check out my GitHub profile and give me a follow. I've gathered all my productivity hacks and expert tips there.