My Git repository is in the wrong root directory. Can I move it? (../ instead of ./)
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.
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
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.
If you used git to solve the problem, any solution would necessarily leave behind a lot of "moved this file here" history entries that aren't actually changes, but you fixing a screwup at creation time. Better to just create it right.
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.)