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

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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 3 - Git

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 4 - Git

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 5 - Git

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 - Git

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

Solution 7 - Git

I came here looking for a way to move my repository anywhere.

In case, I was not the only one, here's what I did in the end:

https://git.wiki.kernel.org/index.php/GitFaq#How_do_I_clone_a_repository_with_all_remotely_tracked_branches.3F I "git clone --mirror"d a bare copy of my repo and afterwards told it to not be bare anymore, so the files showed up in that new folder. (Then checked whether or not files and log had appeared.) Kept the old repo for a while, just in case...

That way I was able to move my repo without losing history.

Best Greetings, Dinah

Solution 8 - Git

Having just gone through this same problem my eventual solution was:

  1. Move the .git folder to where it needed to be.
  2. Change directory to the folder that I have just moved .git to
  3. Reset the folder contents to what git thinks that it should be: git reset --hard HEAD
  4. Check that the contents match what they should be with kdiff3 or another compare tool
  5. Delete the now un-versioned files at the old location.

It worked like magic with no impact on the history. - N.B. If you have made some changes be sure to commit them before the move of the .git directory.

Solution 9 - Git

There are two ways here:

> 1. cd TheWrongDirectory rm -rf .git > > 2. just DELETE the .git folder and cd to the right directory.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikeView Question on Stackoverflow
Solution 1 - GitAbhishek AnandView Answer on Stackoverflow
Solution 2 - GitholmesalView Answer on Stackoverflow
Solution 3 - GitndimView Answer on Stackoverflow
Solution 4 - GitT.E.D.View Answer on Stackoverflow
Solution 5 - GitM. Reza NasirlooView Answer on Stackoverflow
Solution 6 - GitjkndrknView Answer on Stackoverflow
Solution 7 - Gituser1565849View Answer on Stackoverflow
Solution 8 - GitSteve BarnesView Answer on Stackoverflow
Solution 9 - GitV. RanceView Answer on Stackoverflow