How I Automated Git Commands Using Batch Scripting

posted 5 min read

In this article, I will show you how I automated Git commands using Batch Scripting on Windows.

As a developer, there are some Git commands that are very fundamental to my work. However, it gets tedious many times when you have to fire up your terminal, type in git commands, and then hit "Enter" on the keyboard for to run each command. Sometimes this process is repeated up to 10 times a day.

Therefore, I thought there must be a way to automate Git commands so that I do not have to manually type and run Git commands again.

I know, some might say why not just use Git desktop instead? Well, I love the terminal, and how it does not abstract my interaction with Git and Github.

So, I have written a Batch Script to automate my most used Git commands on my Windows machine and make my life a little easier. But first, what is Batch Scripting?

Batch Scripting Explained

Batch scripting refers to creating scripts (text files containing commands) for the Windows command prompt (cmd.exe) using the batch file language. These scripts have a .bat or .cmd file extension and automate repetitive tasks by executing a series of commands sequentially.

Key Characteristics of Batch Scripting:

  • File Extension: .bat or .cmd

  • Interpreter: Windows Command Processor (cmd.exe)

  • Purpose: Automates command sequences that would otherwise be typed manually

  • Syntax: Simple, line-by-line command execution with some programming constructs

Common Uses of Batch Scripting:

  • Automating file operations (copy, move, delete)

  • Installing software silently

  • System maintenance tasks

  • Launching programs in sequence

  • Simple system administration tasks

Advantages:

  • Easy to learn and use

  • No special tools required (just Notepad)

  • Built into all Windows systems

  • Quick for simple automation tasks

Limitations:

  • Limited programming features compared to PowerShell or Bash

  • No advanced error handling

  • Windows-specific (not cross-platform)

Now that we know what Batch Scripting is, let me show you how I used it to automate Git CLI on my Windows computer.

My most used Git commands are as follows:

  • git init
  • git remote add origin
  • git branch -M
  • git add .
  • git status
  • git commit -m
  • git push -u origin main

True, these are basic Git commands (and I use much more commands than these). However, because Batch Scripting runs sequentially, these are /the commands that typically follow one another when creating and pushing code/files to a Github repository; making them the easiest commands to automate.

That said, you can also write Batch Scripts to independently automate each Git command if you wish.

Batch Scripting Git Commands

Here is the Batch Script that I used to automate the above Git commands:

@echo off
:: Clear the screen
cls

set /p choice="Enter name of folder: "

:: Changes into the directory of interest
cd "%USERPROFILE%\Documents\%choice%"

:: Ask if the user wants to initialize Git
set /p git_init="Initialize Git? (yes/no): "
if /i "%git_init%"=="yes" (
    git init
)

:: Get the Git remote repository URL
set /p git_remote_url="Enter Git remote repo URL: "
if not "%git_remote_url%"=="" (
    git remote add origin "%git_remote_url%"
)

:: Get the branch name
set /p git_branch="Name of branch? "
if not "%git_branch%"=="" (
    git branch -M "%git_branch%"
)

:: Ask if the user wants to add files to staging
set /p git_add="Add to staging area? (yes/no): "
if /i "%git_add%"=="yes" (
    git add .
)

:: Get the commit message
set /p git_commit_message="Enter commit message: "
if not "%git_commit_message%"=="" (
    git commit -m "%git_commit_message%"
)

:: Ask if the user wants to push
set /p git_push="Push to branch '%git_branch%'? (yes/no): "
if /i "%git_push%"=="yes" (
    git push -u origin "%git_branch%"
)

:: Pause the script to view results
pause

The script begins by ensuring the terminal output is clean and easy to follow. It disables command echoing with @echo off and clears the screen using cls. This provides a tidy interface for the user.

Next, the script prompts the user to specify the name of a folder located in their Documents directory:

set /p choice="Enter name of folder: "
cd "%USERPROFILE%\Documents\%choice%"

This dynamic approach allows the script to work with any project folder the user specifies, without hardcoding paths.

Once inside the target directory, the script offers to initialize a Git repository. Using a simple yes/no prompt, the user can choose to run git init. The inclusion of /i in the condition makes the comparison case insensitive, accepting "yes", "YES", or "Yes" equally:

set /p git_init="Initialize Git? (yes/no): "
if /i "%git_init%"=="yes" (
    git init
)

Following initialization, the user is prompted to provide a remote repository URL. If a URL is supplied, the script adds it as the origin remote:

set /p git_remote_url="Enter Git remote repo URL: "
if not "%git_remote_url%"=="" (
    git remote add origin "%git_remote_url%"
)

The script then moves on to branch management. It asks the user to name their branch and, if provided, renames the current branch accordingly:

set /p git_branch="Name of branch? "
if not "%git_branch%"=="" (
    git branch -M "%git_branch%"
)

Then, it collects a commit message and, if one is entered, creates the initial commit:

set /p git_commit_message="Enter commit message: "
if not "%git_commit_message%"=="" (
    git commit -m "%git_commit_message%"
)

Finally, the script offers to push the changes to the remote repository. If the user agrees, it pushes the commits to the specified branch, setting the upstream branch for future pushes:

set /p git_push="Push to branch '%git_branch%'? (yes/no): "
if /i "%git_push%"=="yes" (
    git push -u origin "%git_branch%"
)

At the end of the script, pause keeps the terminal window open, giving the user a chance to review the output before closing.

In order to write your Batch Script, open any text editor on your computer like Notepad, write your Batch commands, and save it as a .BAT file.

To run the script, simply double-click on it.

To see the Git automation script in action, you can check out this GitHub repository.

You can also download and adapt other automation scripts in the repo for your use case. You can star and contribute to the repository, too :).

I hope this article has helped you get started with automating repetitive tasks on your computer. If yes, then please like and share with others it might benefit.

Here are other helpful resources to get you started with Batch Scripting:

https://www.seobility.net/en/wiki/Batch_Scripting

https://en.ryte.com/wiki/Batch_Programming/

https://www.tutorialspoint.com/batch_script/index.htm

https://exercism.org/tracks/batch

If you read this far, tweet to the author to show them you care. Tweet a Thanks

Nice post, can you write the one using linus, maybe .sh files

I appreciate it. Sure, I could look into automating Linux commands, too.

Thank you, looking for .sh too

Will look into that, thanks.

More Posts

The Ultimate Guide to Mastering Git Commands: Everything You Need to Know

Hanzla Baig Dev - Feb 21

Introducing GitCorteX – The AI-Powered GitHub Automation Tool!

agm024 - Feb 19

How to Achieve Automated Testing Within Sprint?

Shivam Bharadwaj - Feb 6

Introducing Git In A New Light

ByteHackr - Jan 1

Git and GitHub for Python Developers A Comprehensive Guide

Tejas Vaij - Apr 7, 2024
chevron_left