📅  最后修改于: 2023-12-03 15:15:20.406000             🧑  作者: Mango
在 Github 仓库中,添加文件和目录是很常见的操作。当我们需要添加所有文件和子目录时,手动一个一个添加就显得很麻烦。这时候,我们可以通过 Shell/Bash 脚本自动化添加。
要实现自动添加所有文件和子目录的功能,我们需要遍历目录,找到所有需要添加的文件和目录,并通过 Git 命令添加到本地 Git 仓库中。
具体思路如下:
下面是实现自动添加所有文件和子目录的 Shell/Bash 脚本:
#!/bin/bash
# 设置 Git 用户
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
# 遍历根目录及其子目录,找到所有需要添加的文件和目录
find . -type f -or -type d -not -path './.git/*' | while read file; do
# 将所有需要添加的文件和目录按照文件类型分别添加到 Git 仓库
if [ -d $file ]; then
git add $file/
else
git add $file
fi
done
# 将本地 Git 仓库推送到 Github 仓库
git commit -m "Add all files and directories"
git push origin master
上述脚本的具体说明如下:
find
命令遍历根目录及其子目录,排除 .git
目录;if
判断当前文件是否是目录,如果是则添加目录以及其下所有文件和子目录,否则只添加该文件;注意:在执行脚本前,请确保 Git 仓库已初始化并与 Github 仓库关联。另外,如果您使用的是 Windows 系统,请安装 Git Bash 运行 Shell/Bash 脚本。