Make a shell script to update 3 git repos

前端 未结 4 1999
心在旅途
心在旅途 2021-01-31 04:09

I am working with 5 repos that I have cloned in my development environment. When I want to update a git repo, I enter the folder /home/adrian/repo1/ and do:

git checkout

4条回答
  •  别跟我提以往
    2021-01-31 04:48

    I know I'm really late to the party on this question, but here's a little shell script I wrote for this exact purpose.

    It probably seems very amateurish, but that's because it probably is! I mainly wrote this to help myself learn bash, but I hope it helps you (or whoever may be reading this right now).

    There's a lot of unnecessary fluff on this that you can remove (like changing the color of the text, and listing the repositories with uncommitted changes) that you can remove.

    The link to the repo is here

    
    #!/bin/bash
    declare -a my_array
    for f in *; do
        if [ -d "$f" ] ; then
            cd "$f"
            echo -e "\n ------------------ NEW REPOSITORY ------------------\n"
            echo "Now checking $f"
            if [ -d .git ] ; then 
                git add .
                git diff-index --quiet HEAD --
                if [ "$?" -ne 0 ] ; then
                    echo "THE REPO NEEDS TO BE COMMITTED"
                    my_array=( "${my_array[@]}" "${PWD##*/}" )
                fi
                git status
                git push
                git pull
            fi
            cd ..
        fi
    done
    RED=`tput setaf 1`
    reset=`tput sgr0`
    green=`tput setaf 2`
    if [ ${#my_array[@]} -ne 0 ]; then
        var=$(IFS=' '; echo "${my_array[*]}")
        var="${RED}$var${reset}"
        if [ ${#my_array[@]} -eq 1 ]; then
            var="The repository $var"
            var="$var has uncomitted changes."
        else
            var="The repositories $var"
            var="$var have uncomitted changes."
        fi
        echo "$var"
    

提交回复
热议问题