Git Automation Using Shell Scripts

JISHNU N
3 min readMay 12, 2021

The power of shell scripts is only limited by your imagination to code. And git is the most popular version control system out there. So, everyday git push is an overwhelming task at hand for every programmer out there. Some may love the repetition, but I don’t; Even though I don’t push too much. So, I automated the git push with the help of shell scripts ;).

My git automation script is an interactive one. It won’t push everything in a single execution command. In here, I won’t get into much detail about the bash commands. I hope you already know the basics. Else you can always google everything.

1. Git Choice Menu (Clone/Push)

cat <<'EOF'            
Git Choice Menu
------------------------------------------------------- Press 1 - For Cloning the Repo
Press 2 - For Pushing to Repo
------------------------------------------------------- EOF

read user_choice;
case $user_choice in
1)
echo "Enter the git repo URL";
read git_url;
git clone $git_url;

if [ $? -eq 0 ];
then
echo -e "\nClone was successful, Enjoy your Day. \n"; else
echo -e "\nEither URL is invalid, or you've already cloned it here.
\n";
fi ;;

2) flag=0

In bash $? returns 0 if a command is successful. Else 1. Since bash scripts directly interact with the terminal, we don’t need any other function to execute the git commands.

2. Check whether local Repository exists and add local changes

dir="./.git";if [ ! -d "$dir" ] 
then
echo -e "No Version Control History Found. Initializing the Current Directory \n";
git init;
flag=1;
else
echo -e "\nVersion control history found. \n";
fi
echo -e "\nAdding Files to Push\n";
git add -A;
echo -e "\nPrinting Git Status \n";
git status;
echo -e "\nCan I confirm the push boss (y/n) ?";
read user_input;
echo;

Note: -d ‘filename’; is used to validate whether a directory exists or not. You can also accomplish this using the ‘grep’ command as well.

3. Update Remote repository with local changes

if [ `echo $user_input | grep -iw y` ]
then
echo -e "\nEnter the commit message";
read commit_message;
echo;
git commit -m "$commit_message";if [ $flag -eq 1 ]
then
echo -e "\nEnter the remote git url";
read git_url;
git remote add origin $git_url;
fi
git pull origin main;echo -e "\npushing to branch main \n";git push origin main;if [ $? -eq 0 ];
then
echo -e "\nGit push was successful\n";
else
echo -e "\nGit push failed!.\n";
fi
else
echo -e "\nWrong command. Git push stopped. \n";
fi
;;
*)
echo -e "\nInvalid choice \n";
;;
esac

The rest of the shell script contains git commands wrapped with interactive prints.

To execute the shell script on any directory in your system. You need to create a custom command in bash_aliases for this script execution. This script is used to push from or clone to the current working directory. Execute the following command to edit bash_aliases.

nano ~/.bash_aliases

And add

alias gitbot='source ./path';

The path must be the location of this shell script on your system. Now when you enter the command ‘gitbot’ on the terminal the script will be executed on the current working directory.

Now you can leave without getting roasted :).

You can also use any other programming language that has functions to access the terminal commands to automate git. In python, you can use the system method from the os module.

os.system('terminal command');

It’s also possible to run this shell script in windows, with the help of windows bash shell. A complete OS independent program can be easily coded with python too. Since I’m a Linux person, I’m gonna stick with bash :).

You can find the complete code in here.

--

--

JISHNU N

A passionate programmer, who can’t focus on one thing at a time !