Debug School

rakesh kumar
rakesh kumar

Posted on

Shell script to git add and commit and push for speified dir at a time with example

#!/bin/bash

if [ $# -eq 0 ]; then
    echo "Please specify a directory name"
    exit 1
fi

dir=$1

if [ ! -d "$dir" ]; then
    echo "Error: directory $dir does not exist"
    exit 1
fi

cd "$dir"
git add .
git commit -m "Adding changes in $dir"
git push origin master
cd ..
Enter fullscreen mode Exit fullscreen mode

This script takes the first argument passed to it ($1) as the name of the directory that you want to add, commit, and push changes for. It first checks that a directory name has been specified (if [ $# -eq 0 ]), and if not, prints an error message and exits the script (exit 1). Then, it checks that the specified directory exists (if [ ! -d "$dir" ]), and if not, prints an error message and exits the script (exit 1). If the specified directory exists, the script changes into the directory (cd "$dir"), adds all files (git add .), commits the changes with a message indicating which directory the changes were made in (`git commit -m "Adding changes in

Top comments (0)