My Git repository is in the wrong root directory. Can I move it? (../ instead of ./)
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.
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
This worked for me, and kept all my history intact. From the incorrect root folder (the parent where you accidentally initialized the repo):
Move the folder:
mv .git thecorrectfolder/
Re-initialize the git repo:
cd thecorrectfolder/
git init
Re-add all the files, commit, and push:
git add .
git commit -am 'fixing things'
git push origin master
Done! Get yourself a beer.
When you commit the git repo after re-initializing, you'll get a bunch of output that looks like this:
rename {ethanode/coffee => coffee}/app.coffee (100%)
In other words, all of your references from the parent folder and being renamed to use the correct folder.
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.)