Moving a git repository up one hierarchy level

Sorcy picture Sorcy · Mar 23, 2011 · Viewed 30k times · Source

Git beginner question:

I have a small private webproject which is versioned locally with msysgit. There is no exterior repository, as it's only for me, so i can bascially do whatever I want.

I've had this set up in the project directory, ie in "webroot".

Now a second directory had to be created, placed parallel to webroot. Let's call it assets.

So structure is now as follows:

\ project directory
----\webroot
----\assets

I'd love to include this new directory in the git repository, so that I'd also version changes to files stored there, but of course I can't use "git add ../assets". Neither am I inclined to create a new git project in project_directory, as this would loose all my previous commits.

So how do I go about moving the repository out of "webroot" up into "project_directory", while keeping my commits and then being able to include "assets"?

Answer

Tim Henigan picture Tim Henigan · Mar 23, 2011

So, you want your git repo to look like this:

<projectdir>
    /.git
    /webroot
    /assets

To do this, you must move the existing files in your repo into a new webroot subdirectory.

cd <git repo root>
mkdir webroot
git mv <all your files> webroot
git commit --all -m "moved all existing files to new 'webroot' directory"

Then, on your local filesystem you want to relocate your clone one directory above where it is now:

cd <projectdir>
mv webroot/* .
rmdir webroot

Then you want to add the assets directory (and files) to the git repo:

git add assets
git commit -m "added assets to the repo"