首页 > 解决方案 > 如何将所有 git repo 从目录推送到 shellscript 中的 git

问题描述

我正在尝试编写一个 bash 脚本来查找新的 repo 和文件并提交并推送它们,以及我添加到我的项目文件夹中的未来 git repo。

为了简单起见,我所有的 git 项目都存储在我系统上的一个文件夹中。

我面临的问题是,每当我尝试使用 1 班轮查找所有 .git 文件夹并在它们上运行命令时,它都无法在需要它的文件夹上执行所有 3 个 git 命令(添加、提交、推送)

我尝试运行不同版本的脚本,一些在 git push 中使用 &,一些使用 && 但它仍然只添加和提交带有更改的 repo,但它不会推送它们。

我还尝试将它变成一个函数,让它作为一个单独的命令运行,但我仍然在各处收到错误消息。

我的脚本如下所示:

# Location of all my github projects
mygit=$HOME/github/myrepos

addcompush="git add . && git commit -a -m "Uploaded by script, no commit msg" & git push"

# Find all git repo folders and run git add + git commit + git push on them
find "$mygit" -name ".git" -type d -exec bash -c "echo '{}' && cd '{}'/.. && $(addcompush)" \;

如上所述,我还尝试了不同的版本:

find "$mygit" -name ".git" -type d -exec bash -c "echo '{}' && cd '{}'/.. && git add . && git commit -a -m "uploaded by script" && git push" \;

这是 git add 和 commit 但它不会推送它们,我怀疑这是由于 &&. 但我完全不知道如何解决这个问题。

我是否需要重新调整我的整个处理方式,或者我可以按原样完成这项工作吗?

标签: bashgitshellgithubautomation

解决方案


在这里发布我自己的解决方案,不是最优雅的解决方案,但它有效。如果在 1 个文件夹中没有要提交的内容,我在 atm 中看到它的方式会中断 find 命令。

因此,我发现制作一个以比 1 班轮更直接的方式处理执行的脚本会更好。

# Location of the git folder collection
mygitfolder=$HOME/github/myrepos

# Loop through all folders and place the names in an array
git_folders=()
while IFS= read -r line; do
    git_folders+=( "$line" )
  done< <(ls ~/github/myrepos/)

# Loop through the array and run the commands needed to automaticly push a repo to github,
# git add, commit, push(IF commit executed sucsessfully meaning it was something to commit in that folder) 
for folder in "${git_folders[@]}"
do
   cd "$mygitfolder/$folder"; printf "\n\nChecking the $folder repo "
   git add .
   git commit -a -m "Uploaded by script, no commit msg added"
   if [[ $? -eq 0 ]]; then
     git push
   fi
done

推荐阅读