Skip to content

Instantly share code, notes, and snippets.

@ErnestoCobos
Created February 20, 2024 14:27
Show Gist options
  • Save ErnestoCobos/b1878522c7faae5143e47a489f06708a to your computer and use it in GitHub Desktop.
Save ErnestoCobos/b1878522c7faae5143e47a489f06708a to your computer and use it in GitHub Desktop.
Bash script to manage project-specific Git configurations in a JSON file. Easily add or update Git name and email for each working directory. Ideal for developers working on multiple projects with different Git identities. Utilizes jq for safe JSON manipulation.
#!/bin/bash
# This script updates Git configurations for different projects stored in a JSON file within the user's .config directory
# Define the path to the JSON configuration file located in the .config directory of the user's home directory
CONFIG_FILE="$HOME/.config/git-configs.json"
# Check if the configuration file exists. If it does not, create a new JSON file with empty object initialization
if [ ! -f "$CONFIG_FILE" ]; then
echo "{}" > "$CONFIG_FILE"
fi
# Capture the current working directory
DIRECTORY=$(pwd)
# Prompt the user to enter their Git name and email for configuration
read -p "Enter your Git name: " NAME
read -p "Enter your Git email: " EMAIL
# Use jq to add or update the directory-specific Git configuration in the JSON file
# jq is a lightweight and flexible command-line JSON processor
# The script adds or updates an entry for the current directory with the user's Git name, email, and a flag 'applied' set to 'false'
# This entry signifies that the Git configuration specified has not yet been applied to the directory
jq --arg dir "$DIRECTORY" --arg name "$NAME" --arg email "$EMAIL" \
'.[$dir] = {"name": $name, "email": $email, "applied": "false"}' "$CONFIG_FILE" > temp.$$ && mv temp.$$ "$CONFIG_FILE"
# Inform the user that the Git configuration has been updated for the current directory
echo "Git configuration updated for $DIRECTORY"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment