Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

People also ask

Can I move my git repository to another folder?

To do this without any headache: Check what's the current branch in the gitrepo1 with git status , let's say branch "development". Change directory to the newrepo, then git clone the project from repository. Switch branch in newrepo to the previous one: git checkout development .

How do I change to the local directory where I want to clone my repository?

For Windows user 1> Open command prompt. 2> Change the directory to destination folder (Where you want to store your project in local machine.) 3> Now go to project setting online(From where you want to clone) 4> Click on clone, and copy the clone command. 5> Now enter the same on cmd .


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


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.


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


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.