按时间顺序将多个文件夹提交到 git

commiting multiple folders in chronological order into git

我有数百个网站备份,每个文件夹一个。我想将它们放入一个 git 存储库中,每个备份作为一个版本。更改主要涉及图像文件和每天 2 个数据库备份。大小约为 25 GB,并且在增加。
有没有办法告诉 git 获取其中一个文件夹并将其提交到存储库中,然后获取下一个文件夹并提交更改,直到它们全部提交?还是我必须将每个文件夹复制到第一个文件夹,提交并继续下一个文件夹?
文件夹之间的相同文件是硬链接的。
我想用它的时间戳提交每个备份,但据我所知,我可以在每次提交时提供它。
所以我一直在寻找的主要信息实际上是关于指定一个文件夹而不是 git 存储库的一部分并将更改提交到存储库中。

Is there a way to tell git to take one of the folders and commit it into the repository, then take the next folder an commit the changes

没有简单的自动方式,但有一种半自动方式。您必须操纵 GIT_WORK_TREE 或选项 --work-tree。类似的东西:

cd first-version
git init
git add .
git commit -m "first-version"
git --work-tree=../second-version add -A
git commit -m "second-version"

并为每个目录重复。如果所有目录都有规则的名称,您可以 运行 一个循环:

for version_dir in ../*-version; do
    git --work-tree=$version_dir add -A
    git commit -m "$version_dir"
done

I would like to commit them with their timestamps

git 不保留 files/directories 的时间戳,仅保留提交的时间戳。

@phd 的回答是我问题的完美答案。

但是,仅 git 的解决方案在这方面很慢,尤其是行

git --work-tree=../second-version add -A

显然 git 不容易识别相同的文件被添加到暂存中。

因此,由于我在 Linux 系统上工作,我使用 rsync 将我的本地副本与每个文件夹同步。

此外,我对图像文件使用 git lfs。这里的问题是那些文件不会自动移动到lfs系统,所以我必须手动迁移它们。在脚本中,我从本地副本的文件夹中执行以下命令:

rsync -a ../backupfolder_[$NUMBER]/* . --delete

git add -A

git commit -m "$cmessage"

git lfs migrate import --include="folder/imagefolder1/**"

git lfs migrate import --include="imagefolder2/**"

git reflog expire --expire-unreachable=now --all

git gc --prune=now