tags:

views:

339

answers:

4

Somehow when I 'git init'ed my latest project a month or so ago I ran the command in the directory one directory higher than the root of my project.

So my repository is in the ./project directory and not the ./project/my-new-project directory. I don't know how I didn't realize the issue earlier, but I just never looked for the .git directory until now.

Is there a way, without killing my project, to move the repository to the proper directory and then tell git what the new base of the project is? Just moving the directory doesn't work. Git thinks all files have been deleted.

Thanks.

A: 

Use git-mv to move your files "up" to the proper location, then git-rm the "my-new-project" directory.

jkndrkn
It's the other way around. I don't want to move all my files down to the directory git is in (there's tons of other stuff there...it'd be annoying) and then move git up. I want to move the repository up to the root directory of my project.
Mike
+1  A: 

Probably the simplest thing, unless you have already created some history you want to save, would be to just delete the .git subdirectory and redo the init in the correct directory.

T.E.D.
Yeah, that's what I'm thinking. It wouldn't be a huge loss to lose that history, but it would be nice not to if the fix was relatively easy.
Mike
I see what you are saying, but any in-git actions you do to correct this will end up leaving a bunch of "moved this file here" histories that aren't actually changes, but you fixing a screwup at creation time. Better to just create it right.
T.E.D.
+8  A: 

git filter-branch lets you rewrite history in that way. The git filter-branch man page even has your case as an example:

To rewrite the repository to look as if foodir/ had been its project root, and discard all other history:

git filter-branch --subdirectory-filter foodir -- --all

You probably want to git clone the repo into a new subdirectory before (or after?) the git filter-branch run. (Cloning before filter-branch and running the filter-branch on the new clone would have the advantage of leaving the original .git/ dir in place as a backup in case something goes wrong.)

ndim
+2  A: 

I had the opposite problem - had to shift the git root to the parent directory (from project/src to project) To my extreme surprise, the following worked!!

src$ mv .git ../ 
src$ cd ..
project$ git add src
project$ git commit -a

git cleverly detected that all the new files were renamed versions of old ones and no history was lost

You can try something similar... move the .git folder and add the files again before committing

Abhishek Anand