Inputting a list of folder-copied-versions of a project to automatically build a Git repository?

49 views Asked by At

So over the last few years I've really enjoyed learning and using Git for all of my coding projects. I love having a clear timeline of all changes and seeing when a change was made.

Well, I've opened up an old project that predates my Git use. I essentially have a list of folders for each 'commit' I made over time. All together I have over 70 versions. I want to easily store this project while also keeping all of the steps without wasting an atrocious amount of space.

Is there an automated way of doing this? What I am wanting to automatically do is essentially the following:

Folders:

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

To import into a Git repo (single branch):

- 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'
- ...

Without doing this manually, what would be a fast way of accomplishing this? All of the folders are on the same local disk.

2

There are 2 answers

0
Vampire On

This one-liner will do what you want

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 "[]"'
5
Scott Weldon On

Here's a script I threw together that should do the job:

#!/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

Run it as:

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

You could use mv instead of cp, but I have heard that the /. syntax doesn't work with mv. (Also using cp will leave the original files in the unlikely event that something goes wrong.)

Another thing to keep in mind is that depending on the type of project, there may be some files that you don't want tracked by Git. (E.g. binaries.) You may want to add a .gitignore before importing the old copies; if so, modify the script accordingly.

Thanks to @Vampire for pointing out a couple bugs in the original version of my script.