Moving a git repository up one hierarchy level

Solution 1:

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"

Solution 2:

You can also just move your .git dir up one level and update your worktree.

cd projectdir
mv ./webroot/.git ./.git
git config core.worktree /absolute-path-to-project-dir
git add assets
git commit -m 'adding assets folder'

Not positive but I'm pretty sure the path to core.worktree has to be absolute.

Solution 3:

I assume you meant to rewrite the history to contain all the files in all revisions as if they had always been in a subdirectory webroot/ instead of in the root

The git filter-branch manpage has the answer, here an improved version that rewrites all existing refs (branches) and tags:

time git filter-branch --index-filter 'git ls-files -s |
         sed "s-\t\"*-&webroot/-" |
         GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && 
     mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' --tag-name-filter cat -- --all

Care has been taken to make this an index-only operation so that the process will run fast even for big repos. Remember to (when satisfied) get rid of the original refs (.git/refs/original/*) and repack the repo to loose the obsolete tree objects.

Solution 4:

Your commits are not locally tied to the "webroot" folder they are stored within the git repo.

You could simply remove the webroot directory recheckout the repository in the new location "/project directory" add the assets directory and commit.

rm -Rf webroot
git clone path-to-repo
git add assets 
git commit -m "Added assets directory"
git push