【问题标题】:Inputting a list of folder-copied-versions of a project to automatically build a Git repository?输入项目的文件夹复制版本列表以自动构建 Git 存储库?
【发布时间】:2017-05-08 14:26:43
【问题描述】:

所以在过去的几年里,我非常喜欢学习并在我的所有编码项目中使用 Git。我喜欢为所有更改制定清晰的时间表并查看更改的时间。

好吧,我已经打开了一个在我使用 Git 之前的旧项目。我基本上有一个文件夹列表,用于我随着时间的推移所做的每个“提交”。我总共有 70 多个版本。我想在不浪费大量空间的情况下轻松存储该项目,同时保留所有步骤。

有没有一种自动化的方式来做到这一点?我想要自动做的基本上如下:

文件夹:

- '2013_08_01'
- '2013_08_04'
- '2013_08_12'
- ... and many many more (*~70)

要导入 Git 存储库(单个分支):

- Base commit A (+ note of date) of '2013_08_01'
- Commit B with changes (+ note of date) of '2013_08_04'
- Commit C with changes (+ note of date) of '2013_08_12'
- ...

如果不手动执行此操作,那么完成此操作的快速方法是什么?所有文件夹都在同一个本地磁盘上。

【问题讨论】:

  • git 不太可能提供一种自动化的方式来做到这一点。但这应该是一个相当短的 Python 脚本...

标签: git version-control automation version version-control-migration


【解决方案1】:

这个单线会做你想做的事

git init repo && ls -1d 2* | sort | xargs -i[] sh -c 'find repo -mindepth 1 -not -path repo/.git -not -path repo/.git/\* -exec rm -rf {} + && cp -r []/. repo/ && git -C repo add -A && git -C repo commit -am "Commit version []" && git -C repo tag "[]"'

【讨论】:

    【解决方案2】:

    这是我编写的 脚本,应该可以完成这项工作:

    #!/bin/bash
    
    source=$1
    dest=$2
    
    cd $dest
    git init
    
    cd $source
    
    for i in *
    do
      cd $dest
    
      # Remove old version, see https://stackoverflow.com/a/22347541/2747593
      git rm -rf .
      git clean -fxd
    
      # Add next version.
      cp -R $source/$i/. ./
      git add -A
      git commit -m "Auto import to Git, from folder $i"
    done
    

    运行如下:

    script.sh /path/to/source /path/to/dest
    

    您可以使用mv 代替cp,但我有heard 认为/. 语法不适用于mv。 (同样使用cp 将保留原始文件,以防万一出现问题。)

    要记住的另一件事是,根据项目的类型,可能存在一些您不希望 Git 跟踪的文件。 (例如二进制文件。)您可能需要在导入旧副本之前添加.gitignore;如果是这样,请相应地修改脚本。

    感谢 @Vampire 指出我的脚本原始版本中的几个错误。

    【讨论】:

    • 您确定cp $source/$i/. ./ 工作正常吗?这里只是说2013_08_01/. 被省略了。你可能是指cp -r $source/$i/. ./,就像我的解决方案一样?您也不处理文件被删除的情况。使用您的脚本,他们将继续存在于目录中。顺便提一句。你为什么要在你的脚本中使用pushd
    • 现在for i in * 将不再工作,因为它列出了$dest 中的文件和目录,而您仍然不处理已删除的文件。顺便提一句。 for i in * 是否列出保证排序的目录?
    • @Vampire 哎呀,是的,我的意思是cp -rpushd 是先前尝试遗留下来的,已修复。
    • 你的脚本还是有问题,看我的cmets
    • @Vampire 现在应该修复。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    相关资源
    最近更新 更多