2

I have a repository that I created early in the learning curve of a project. As I've learned more, I've realized that this is creating problems because some of the files I want tracked aren't required in the compiled source. What I'd like to do is create a new "project" parent directory, move the existing source directory into that parent directory and the migrate the files that don't need to be compiled into that parent directory.

Is that possible with git? Will I completely destroy all of the references since I'll want to move the .git/ directory up to the parent level as well?

Thanks.

Rob Wilkerson
  • 40,476
  • 42
  • 137
  • 192
  • You say "move the existing source directory into that parent directory and the migrate the files that don't need to be compiled into that parent directory." Which are you moving? And why can't this be done with .gitignore and 'git rm --cached path/to/files' ? – sargas Jun 30 '10 at 19:15
  • 1
    A bit like http://stackoverflow.com/questions/3142419/how-can-i-move-a-directory-in-a-git-repo-for-all-commits (which would mean: no need to move the .git directory)? – VonC Jun 30 '10 at 19:25

2 Answers2

3

One option would be to just move things around within your current repository.

For example, assuming something like:

/.git
/lib/lib.c
/lib/lib.h
/test.c
/readme.txt

You could create a new folder (src below) and move the lib folder and the test.c folder into the new one (using git mv). Then the current directory will be your "project" parent directory that you want, and all the code will be in the new folder.

/.git
/src/lib/lib.c
/src/lib/lib.h
/src/test.c
/readme.txt

An alternative would be git filter-branch, as pointed out by VonC in the comments. Using that I believe you could build a restructured repository that would not have a commit for the moves, the files would just always have existed in the new structure.

Chris Shaffer
  • 32,199
  • 5
  • 49
  • 61
  • I ended up taking this simple, stupid approach after cursing myself mightily for not considering the obvious solution. Thanks, Chris. – Rob Wilkerson Jul 01 '10 at 11:11
1

In agreement with Chris Shaffer, you can use the following script to accomplish those steps:

fullpath=`pwd`
dirname=`basename $fullpath`

if [ ! -d ./.git ]; then
  echo 'Working directory contains no .git repository. There is nothing to move.'
elif [ -d ../.git ]; then
  echo 'Parent directory already contains a .git repository. Aborting.'
elif [ -d $dirname ]; then
  echo "There's already a $dirname directory. Aborting to be safe."

else  # Enough checking; let's get some work done...

  mkdir $dirname
  shopt -s extglob
  for file in `ls -a | egrep -v [.]git\|^[.]+\$\|^$dirname\$`; do
    git mv $file $dirname/
  done
  mv .git* ..
  mv $dirname/* .
  mv html/.[a-z]* .
  git commit -m "Moved Git repository up one directory"
  rmdir $dirname

fi

This might be a little more checking than is needed for a one-off situation, but it should do the job. You can adapt it by hand, or save it as an executable script and then call it from the directory that contains .git.

Apparently others have been able to just mv .git .., but that did not work for me. I have to first perform all the git mv file operations, and then move everything up one.

If I make this any more robust in the future, I'll post updates at https://github.com/jcamenisch/dotfiles/blob/master/bin/mv-git-up .

Jonathan Camenisch
  • 3,472
  • 1
  • 20
  • 9