0

As it turns out, this question asked the same thing Detach (move) 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?

Community
  • 1
  • 1
Alex R
  • 2,201
  • 1
  • 19
  • 32

3 Answers3

2

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.

rfunduk
  • 30,053
  • 5
  • 59
  • 54
  • 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 Dec 30 '09 at 03:57
  • Yes, either of my suggestions will require the subdirs to be git repos. Getting into `filter-branch` stuff is just a recipe for pain. – rfunduk Dec 30 '09 at 04:01
  • 1
    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 – rfunduk Dec 30 '09 at 04:02
  • I should have found that question. It's exactly what I was wondering. – Alex R Dec 30 '09 at 04:33
2

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
  • 134,834
  • 32
  • 188
  • 245
1

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
  • 570
  • 4
  • 11
  • 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 Dec 30 '09 at 04:14