🛠️ Automating Git Repo Updates and Navigation with a POSIX Shell Script

📆 2025-07-10 17:43

Managing multiple Git repositories for different services or components can get tedious. Whether you're working on small personal projects like a blog, a Gemini site, or HTTP microservices, jumping between directories and pulling the latest changes manually is a chore.

That’s why I wrote a simple, POSIX-compliant shell script to:

Let’s break it down.

📝 The Script

Here’s the core of the script:

#!/bin/sh

# Go to working directory
cd /path/to/your/repos

# List of repositories (folders)
repos="finger gemini gopher http"

# Pull latest changes
for repo in $repos; do
    echo "Updating $repo repo"
    cd "$repo" && git pull && cd ..
done

# Display repo menu
createmenu() {
    while :; do
        echo ""
        echo "Select a repo to open:"
        echo "---------------------------"

        i=1
        for repo in $repos; do
            echo "$i) $repo"
            i=$((i + 1))
        done
        echo "$i) Exit"

        printf "Enter your choice (1-$i): "
        read choice

        case $choice in
            ''|*[!0-9]*)
                echo "Invalid input: please enter a number."
                continue
                ;;
        esac

        if [ "$choice" -eq "$i" ]; then
            echo "Exiting..."
            exit
        elif [ "$choice" -ge 1 ] && [ "$choice" -lt "$i" ]; then
            j=1
            for repo in $repos; do
                if [ "$j" -eq "$choice" ]; then
                    echo "Opening $repo..."
                    cd "$repo" && code .
                    exit
                fi
                j=$((j + 1))
            done
        else
            echo "Invalid choice: please select a number from 1 to $i."
        fi
    done
}

createmenu

⚙️ What It Does

🔧 Why POSIX sh?

While Bash offers features like arrays and select, I wanted this script to run in any Unix-like environment, including:

This means no Bash-isms — just pure POSIX sh, making it portable and dependable.

🚀 How To Use

Save the script as update-git-repos.sh

Edit variables

You have to edit only 2 things at the top of the script:

Make it executable:

chmod +x update-git-repos.sh

Run the script

./update-git-repos.sh

Run it with an alias

You can add an alias for the script by adding this line to your shell's config file (e.g., .bashrc, .zshrc, or .profile):

alias update-repos='/full/path/to/update-git-repos.sh'
Read more about Using aliases for complex and daily terminal commands

💡 Final Thoughts

This script has been a small but helpful productivity boost for juggling multiple personal projects. It saves time, avoids repetitive commands, and keeps everything in sync. And because it's written in portable sh, it'll run practically anywhere.

If you find this useful, feel free to adapt it to your own workflow:

The point is: let your tools work for you.

Happy hacking!

🚶 Back to my blog