148

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.

9 Answers 9

290

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

11
  • 8
    This worked perfectly for me. I also moved other .git* config files from the directory like .gitigore Mar 13, 2012 at 23:16
  • 8
    My dear sir, you're a life saviour. This thing really WORKS. Thank you. Jul 18, 2013 at 17:47
  • 1
    Additionally, I git rm'd the files from their old location, so git status (correctly) ended up reporting a number of rename operations. The more I work with git, the more I like it.
    – ssc
    Jan 13, 2014 at 13:03
  • 7
    @Mike this should be marked as the accepted answer. It's a preferred solution to re-initing because you don't lose history. If you don't believe me, the upvote count speaks for itself. May 11, 2014 at 13:34
  • 5
    Thank you! It worked for me, but I had to do a couple more things: 1. Before committing, I updated .gitignore and ran git add -A in the root folder; after this, git correctly showed all files as renamed rather than deleted. 2. Since I use submodules, I had to move .gitmodules to the new root, and update the submodule path in the various git configuration files. To find which files to modify I ran grep -nrI --color 'old/path/to/submodule' . Nov 8, 2017 at 15:29
46

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.

2
  • This worked for me, but it was slightly ugly. As far as git's concerned, I didn't 'move' anything, I just deleted hundreds of files and then added hundreds of other, identical files.
    – bjmc
    May 27, 2014 at 6:49
  • 8
    git init is not required and the remaining part is same as my answer which was submitted years before yours: stackoverflow.com/a/3247756/391753 Jun 7, 2014 at 0:58
34

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

2
  • 1
    This doesn't "discard all other history" (suggesting the .git repo stays at top level while subdirectories become "thought of as the root") - this will literally discard all other files in the repo, and move all subdirectory/* files up to the root folder. Nov 9, 2015 at 22:58
  • Great answer, but keep in mind that if there's a .gitignore file (if tracked), it will be lost with this operation. Also, you'll need to do a git push --force to update the upstream repository after this.
    – waldyrious
    Dec 12, 2016 at 15:58
26

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.

5
  • 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
    Dec 16, 2009 at 22:29
  • 8
    This breaks a lot of stuff. stackoverflow.com/a/3247756/825364 is a much better way to do it. Jun 9, 2015 at 12:52
  • 3
    Well, saving some history is what Git is used for. Maybe we should just delete the .git subdirectory and uninstall Git from our system? Using an ftp-server instead of vcs is probably the simplest thing after all!
    – Gherman
    Dec 8, 2015 at 12:09
  • 3
    the answer below is a much better approach since it retains all the history
    – BigMikeW
    Jul 5, 2017 at 1:16
  • 2
    So for me, I did this and immediately realized my mistake. without having any changes to worry about, this is the easiest way to get back to a clean slate.
    – Mike
    Aug 3, 2017 at 15:43
13

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."
3

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

1
  • 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
    Dec 16, 2009 at 22:11
2

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

1
  • Correct me if I'm wrong, but it appears that you could have achieved that by simply moving the repository directory to its designated position using nothing but bare filesystem tools, like mv /path/to/the/old/location/of/the/repo /path/to/the/new/location/of/the/repo.
    – cueedee
    Feb 25, 2020 at 9:37
2

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.

-3

There are two ways here:

  1. cd TheWrongDirectory rm -rf .git

  2. just DELETE the .git folder and cd to the right directory.

Not the answer you're looking for? Browse other questions tagged or ask your own question.