如何在目录中将所有git repo从目录推送到git

问题描述 投票:1回答:1

我正在尝试编写一个bash脚本,以查找新的回购和文件,然后提交并推送所有这些文件,以及将来添加到我的项目文件夹中的git回购。

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

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

我尝试运行脚本的不同版本,其中一些在git push上使用&,有些在&&上运行,但它仍然仅添加并提交带有更改的仓库,但不推送它们。

我还尝试过使其成为一个函数以使其作为单独的命令运行,但是我仍然到处都收到错误消息。

我的脚本看起来像这样:

# 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的添加和提交,但不会推送它们,我怀疑这是由于&&引起的。但是我对于如何解决这个问题完全迷失了。

我是否需要重组我的整个处理方式,还是可以按原样进行这项工作?

bash git shell github automation
1个回答
0
投票

在这里发布我自己的解决方案,不是最优雅的解决方案,但是它可以工作。如果在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
© www.soinside.com 2019 - 2024. All rights reserved.