Moving a git repository up one hierarchy level

六月ゝ 毕业季﹏ 提交于 2019-11-27 00:34:14

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"

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.

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.

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

The following command would rewrite your Git history. It would look like as if the content was in webroot all along. Usually rewriting history is troublesome if multiple people are working with a repo. Since you are working alone on it, it should be fine.

git filter-branch --index-filter '
    git read-tree --prefix="webroot/" $GIT_COMMIT && \
    git ls-files \
      | sed "s/\/.*//" \
      | sort \
      | uniq \
      | grep -v "^webroot" \
      | xargs -L1 git rm -r --cached > /dev/null'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!