1

所以在过去的几年里,我真的很喜欢在我所有的编码项目中学习和使用 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'
- ...

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

4

2 回答 2

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 "[]"'
于 2016-12-22T23:50:41.917 回答
0

这是我拼凑起来的一个

#!/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,但我听说/.语法不适用于mv. (同样使用cp将保留原始文件,以防万一出现问题。)

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

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

于 2016-12-22T23:07:47.440 回答