Automate git commands with bash script

BASH GIT

In this article we will create a small bash script which can handle basic git commands (add, commit and push) in order to help development process on smaller basic projects.


Development process

Basic GIT commands like git add, git commit and git push are frequently used during a development process. Moreover in some cases we do not use any third party application like GitKraken or SourceTree, we simply use the command line. In a small project when we are basically only developing for ourselves, using only one branch (which can be the master or whatever branch) we tend to commit and push all modifications by “one-turn” – so the main goal is to store the code in the remote repository as well to help us work from multiple locations.

In this case it can be repetitive process to always enter all commands one after one. To handle this problem, we can create a bash script in the repository’s root and call that one script in case we want to commit and push the code.

Creating the script

Create the file

Make sure that this script is located in the root of your repository, so I suggest to use CLI and cd to that folder. We will create a bash script named upload.bash, but whatever name can be used.

cd route/to/the/repository
touch upload.bash

Add the script

After it’s created, open the file with a preferred text editor (I suggest VS Code) and add the following lines below. Note that this script will add all new files and all modifications at once using the git add . command. If you want to separate commit messages you may modify the script itself or execute the command after you modified all the neccessary files which belongs together in terms of commit message.

#!/bin/bash
git add .
git commit -m "$1"
git push
printf "\n\n>>> UPLOAD DONE\n\n"

Execute the script

We can run the file by entering this command to the CLI:

bash upload.bash "some string which will be used as commit message"

Sidenotes

Alias

The above method is not a fully working solution, because each time you have to clone/copy the bash script. To overcome this problem an alias should be created eg. alias bashpush bash upload.bash and then it will be available from other places. (In this case make sure that upload.bash is available system-wide!)

Bash shell

Make sure that you have bash shell on your computer. If using Linux or MacOS it’s no problem. On Windows I suggest to download git Bash and use it to execute these commands. Keep in mind that starting from MacOS Catalina Apple replaced bash shell with zsh shell – which is amazing.

Frequent usage

I suggest to create your own public repository (or use mine) to download this file easily in all your repositories so then you will be able to run this script easily in any case and time. Using dedicatied repository and cloning it, make sure that the .git folder does not conflicts with your original / base repository.

git clone https://gitlab.com/siposmhu-blog-projects/git-bash-script.git