How to use bash on Windows to easily update a GIT project

How to use bash on Windows to easily update a GIT project

Despite general thinking, I love to develop on Windows instead of an Unix-like system. So, after I found out that I can create and use aliases to run scripts on my Windows terminal and automate the annoying task of updating my GIT repository, I decided to write this post.

Setup

The requirement to update a GIT repository is, of course, to have the GIT bash installed on the system.

The next step is to create a .bashrc file (yes! Like the Unix-like systems) in your Users folder (in my case is C:\Users\). We will work on this file after explaining the script in the next section.

Script

So, my natural action after fixing a bug in a side project is just to add all the files I changed, confirm as a bug fix and push. A simple script as shown below does the job:

echo Starting Git-updater V1.0
echo
echo git add .
git add .

if [ "$1" != "" ]
    then
    echo git commit -m "$1"
    git commit -m "$1"
else
    echo git commit -m "bugfix"
    git commit -m "bugfix"
fi

if [ "$2" != "" ]
    then
    echo git push origin "$2"
    git push origin "$2"
else
    echo git push origin main
    git push origin main
fi
echo END

I saved it as up.sh in my D:/scripts folder.

In this script, we have two if statements, the first is to change the push commit message. If nothing is white, the default “bugfix” commit is used. The second 'if' is to choose the branch, if no value is used, the push will use the "main" branch.

Creating the alias

We have our script ready to work, now it's time to define the alias (make the terminal understand a command using only a keyword). Back to our .bashrc file, open it in any editor (it can be notepad) and create a new alias. I decided to use the 'up' alias (update shortcut), so if the keyword 'up' is used in my terminal, I want to run a shell script located in my D:/scripts folder. The result of this is shown below:

alias up="sh D:/scripts/up.sh"

Examples

The script works in three ways:

  1. up -> create a commit named “bugfix” and push to the main branch
  2. up “bugfix on resize” -> create a commit named “bugfix on resize” (pay attention to the double quotes) and push to the main branch
  3. up “bugfix on resize” “develop” -> create a commit named “bugfix on resize” and push to develop branch

In the image below we can see it working in one of my projects, a simple 'up' keyword in my terminal update my git project.