首页 > 解决方案 > Git 通过 bash 脚本推送到各种存储库

问题描述

我正在尝试创建一个 bash 脚本,该脚本迭代 4 个不同的文件夹(git 存储库)并更新/推送每个文件夹的更改。我的脚本大部分都完成了,除了身份验证部分。

这是我当前的脚本:

#!/bin/bash

# Fetch username and password
echo "Please insert your git credentials!"
read -p 'Username: ' username
read -sp 'Password: ' password

# Check if you svn and git installed in your machine
if  which svn &> /dev/null && which git &> /dev/null; then

    # Store the current dir
    CUR_DIR=$(pwd)

    # Let the person running the script know what's going on.
    echo "Pulling in latest changes for all repositories..."

    for D in $CUR_DIR/*; do
        if [ -d "${D}" ]; then
            echo "${D}"

            cd $(basename $D);

            # Make sure SVN is on trunk branch and git branch is on master
            SVN_BRANCH=$(svn info | grep '^URL:' | egrep -o '(tags|branches)/[^/]+|trunk' | egrep -o '[^/]+$')
            GIT_BRANCH=$(git symbolic-ref --short HEAD)

            if [ "$SVN_BRANCH"!="trunk" ] || [ "$GIT_BRANCH"!="master" ]; then
                echo $CUR_DIR
                echo "Make sure you're on SVN trunk branch and git master branch."
                exit -1
            fi

            echo "Update SVN repository";
            svn up

            # Update git
            git add .;
            git commit -m "Update with the changes from svn trunk branch."
            echo "Start pushing changes to main repository."
            git push origin master

            echo "Update git repository";
            git pull origin master

            # Update SVN
            svn add .
            svn commit -m "Update with changes from git master branch."

            cd ..
        fi

        echo "Complete!"
    done
else
    echo "You need both svn and git CLI's installed to run this script!";
fi

如您所见,我已经在脚本开头获取凭据,我的问题是如何使用它们来执行需要身份验证的操作,即

git push origin master
git pull origin master

如何构建 git 命令来发出经过身份验证的请求?

标签: bashgitscripting

解决方案


推荐阅读