1

Following a course in iOS development, I had several assignments. After completing each assignment, I made a copy of the complete project directory. So, now after 4 assignments, I have 4 directories, each with a snapshot of how the project was at that time.

Now I want to start a git repository, and have the "full" history there. I want to start in the first directory, i.e. with the first version of the project, push everything, and then push the differences between the first and the second, etc. But since they're in different directories, I guess I can't simply "git remote add origin" in one after the other and "add --all", "commit", and "push"? Because with "git add --all" I'd push everything all over, not the differences?

Clarification: None of the directories are git repository yet

Arjan
  • 13,972
  • 4
  • 26
  • 35
  • possible duplicate of [How do you merge two git repositories?](http://stackoverflow.com/questions/1425892/how-do-you-merge-two-git-repositories) Note that there are quite a few answers that are all relevant there, so make sure you don't just read the first one. – Thomas Orozco Oct 06 '14 at 07:45
  • @ThomasOrozco But, the different directories are not Git repos yet, so I guess not? – Arjan Oct 06 '14 at 08:00

1 Answers1

2

If those project directories are not git repos themselves, the easiest way is to use the --work-tree option of the git command:

git init newRepo
cd newRepo

git --work-tree=/path/to/project1 add -A .
git commit -m "Project1"

git --work-tree=/path/to/project2 add -A .
git commit -m "Project2"

The second add command will only add the delta between project1 (already versioned) and project2.

At the end, you would need indeed a git reset --hard, in order to make your working tree (of your actual git repo) in sync with the index.

Note that any git status during the process would not work (in that it would compare your local empty working tree with the index, and would display deletions as a result).

A 'git --work-tree=/path/to/projectx status' would work though.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • I tried this, seems to work greatly, but, it also creates a list of unstaged "deleted:" changes, containing "delete:"s for everything I added. If I would commit those, it would revert all additions, so after the last commit I did a "reset --hard" to prevent this, and now it seems to be OK. Is that normal behaviour? – Arjan Oct 06 '14 at 09:27
  • @Arjan actually, I would use git -A . instead of git ., in order to record addition, modification *and* deletion. I have edited the answer. No reset should be needed. – VonC Oct 06 '14 at 09:30
  • The same thing persist then: http://pastebin.com/qU7GsRE7 - It works great, but only if I ignore the unstaged "deleted:" until the last commit and then "reset --hard". – Arjan Oct 06 '14 at 09:54
  • @Arjan I have edited the answer. The `git reset --hard` step is indeed mandatory. Be aware that any `git status` or `git add` command wouldn't work unless you use the proper `--work-tree=/path/to/projectx` option for the `git` command. – VonC Oct 06 '14 at 10:56