tags:

views:

121

answers:

3

As it turns out, this question asked the same thing http://stackoverflow.com/questions/359424/detach-subdirectory-into-separate-git-repository

I decided to learn git and to keep my programming homework in a git repo. I think it was a good idea. Now I'm wondering about something:

I had a single git repo with a subdirectory for each assignment. I'm wondering if there's a sane way to make each of the subdirectories into its own git repo, retaining the history associated with those files. It seems like "git filter-branch --subirectory-filter" is part of the answer, but I'm not sure what to do.

EDIT

To clarify: My structure is like this

superdir  
  .git  
  subdir1
  subdir2
  subdir3

I'd like my structure to be more like this

superdir    
  subdir1
    .git
  subdir2
    .git
  subdir3
    .git

Can I do it?

+2  A: 

You're probably looking for git submodules, but I'd just change your layout to have the top level not be a git repo and just have the subdir repos managed independently.

thenduks
So I thought about that, but from what I understand, that would require the subdirectories to already be their own git repositories; am I wrong?
Alex R
Yes, either of my suggestions will require the subdirs to be git repos. Getting into `filter-branch` stuff is just a recipe for pain.
thenduks
You could do a one-time run through of all your stuff and make them repos using something like the process described in the accepted answer here: http://stackoverflow.com/questions/359424/detach-subdirectory-into-separate-git-repository
thenduks
I should have found that question. It's exactly what I was wondering.
Alex R
A: 

Why not place each class as it's own repo and each assignment as it's own branch? As I look back on my college coursework, that is how I would organize things.

Andrew
That might make more sense. Again, my initial configuration was not very good. I'm wondering if I can reconfigure it in a better way.
Alex R
+2  A: 

Use a script such as the one below:

#! /bin/bash

superdir=file:///tmp/superdir
subdirs=(subdir1 subdir2 subdir3)

for dir in ${subdirs[@]}; do
  echo "Rewriting $dir..."
  git clone --quiet $superdir "$dir" || exit 1
  cd "$dir"

  # workaround for git-1.6.4.2 on Cygwin 1.7
  # otherwise, git-filter-branch complains about a dirty branch
  git reset --hard -q

  git filter-branch --subdirectory-filter "$dir" HEAD >/dev/null
  git reflog expire --expire=0 --all
  git gc --quiet --prune=0
  cd ..
done

Starting with a superdir repo whose structure is

$ ls -a . *
.:
.  ..  .git  subdir1  subdir2  subdir3

subdir1:
.  ..  file1

subdir2:
.  ..  file2

subdir3:
.  ..  file3

the result is

$ ls -a . *
.:
.  ..  subdir1 subdir2  subdir3

subdir1:
.  ..  .git  file1

subdir2:
.  ..  .git  file2

subdir3:
.  ..  .git  file3
Greg Bacon