My Git repository is in the wrong root directory. Can I move it? (../ instead of ./)

My Git repository is in the wrong root directory. Can I move it? (../ instead of ./)

Somehow when I git inited 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.

Solutions/Answers:

Solution 1:

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.

Solution 2:

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

Related:  Git bisect with merged commits

Solution 3:

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.

Solution 4:

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.)

Solution 5:

Git can remember files with their hashes,

Just move your .git to root directory and tell git to remember all files changes with --all option.

$ mv .git ../
$ cd ..
$ git add . --all 
$ git status // => you can see all the files recognized as renamed 100%
$ git commit -m "Moves repo to root directory."

Solution 6:

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

Related:  When do I need to do “git pull”, before or after “git add, git commit”?

References