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.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Mike
  • 1,547
  • 2
  • 11
  • 7

9 Answers9

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

sth
  • 222,467
  • 53
  • 283
  • 367
Abhishek Anand
  • 3,789
  • 2
  • 21
  • 29
  • 8
    This worked perfectly for me. I also moved other .git* config files from the directory like .gitigore – Relequestual Mar 13 '12 at 23:16
  • 8
    My dear sir, you're a life saviour. This thing really WORKS. Thank you. – Radu Murzea Jul 18 '13 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 '14 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. – Joseph Spens May 11 '14 at 13:34
  • 1
    the history is kept but not cleanly – Alex R Aug 27 '14 at 02:35
  • @AlexR : are you refering to the fact that the old commits still reference old paths of the moved files? – Abhishek Anand Aug 28 '14 at 16:10
  • I'm using Git 1.9.1 which I think is the default for Ubuntu 14.04. I do not get renames. After mv .git ../../../.. and git add src/com/domain/package I see only deleted .java files and new .java files. – H2ONaCl Jan 10 '16 at 01:49
  • @H2ONaCl Can you try a very small moving example on a toy repo, and post all the steps (preferably command-line) to reproduce the problem? You should also include the steps for creation of the toy repo. – Abhishek Anand Jan 10 '16 at 15:10
  • @AbhishekAnand After doing your method, how can you later checkout to a earlier version of the program? If I do every file is moved/appears where the .git is now. – pablo_worker May 02 '17 at 16:03
  • 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' . ` – coccoinomane Nov 08 '17 at 15:29
  • While it is true to correctly create git the first time (as explained by @T.E.D), we do not always have this luxury. With that said @AbhishekAnand answer is most correct. – Potion Oct 18 '19 at 01:08
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.

holmesal
  • 722
  • 6
  • 11
  • 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 '14 at 06:49
  • 8
    git init is not required and the remaining part is same as my answer which was submitted years before yours: http://stackoverflow.com/a/3247756/391753 – Abhishek Anand Jun 07 '14 at 00: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.)

ndim
  • 35,870
  • 12
  • 47
  • 57
  • 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. – Louis Maddox Nov 09 '15 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 '16 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.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • 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 '09 at 22:29
  • 8
    This breaks a lot of stuff. http://stackoverflow.com/a/3247756/825364 is a much better way to do it. – Steve Tauber Jun 09 '15 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 08 '15 at 12:09
  • 3
    the answer below is a much better approach since it retains all the history – BigMikeW Jul 05 '17 at 01: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 03 '17 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."
M. Reza Nasirloo
  • 16,434
  • 2
  • 29
  • 41
3

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

jkndrkn
  • 4,012
  • 4
  • 36
  • 41
  • 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 '09 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

user1565849
  • 102
  • 1
  • 5
  • 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 '20 at 09: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.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
-3

There are two ways here:

  1. cd TheWrongDirectory rm -rf .git

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

Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
V. Rance
  • 1
  • 3