Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run a git commit inside bash script for loop for multiple repos

Tags:

git

bash

I am trying to write a script to make my bulk - git collaboration easier, to push / pull / or commit multiple git projects at once.

I work on multiple sites, and I want to pull all of the current changes for all of the sites at once.

Here is what I have.

#!/bin/bash -e

REPOS=(
/Users/me/repo1
/Users/me/repo2
/Users/me/repo3
) 

echo push, pull or commit?

read input

if [$input -eq "commit"]
then
    for i in “${REPOS[@]}”
    do
        cd $i
        git add . -A
        git commit -m "auto commit backup point"
        echo "Moving to Next REPO...
         "
        sleep 2
    done 

else 
for i in “${REPOS[@]}”
do
    cd $i
    git $input 
    echo "Moving to Next REPO...
     "
    sleep 2
    done 
fi

Now, when the script prompts me for input, push or pull is working fine (sort of), the script runs through all of sites and pulls or pushes accordingly.

However when I respond "commit" the script goes through each repo, but the commits are not working properly. The Commit message prompt opens in vi which is not preferred, and the commit doesnt place. If I navigate to any of the repos there are still staged changes.

any input would be greatly appreciated.

like image 800
RobBenz Avatar asked Sep 26 '26 02:09

RobBenz


1 Answers

if [$input -eq "commit"] has missing space. Rewrite it as if [ $input -eq "commit" ]. Otherwise it goes to else condition and does git $input, where input=commit.

Oh, yes, -eq is for integers, should be if [ $input = "commit" ].

like image 166
kan Avatar answered Sep 28 '26 20:09

kan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!