将git存储库移动到一个层次结构级别

时间:2011-03-23 16:26:08

标签: git repository hierarchy

Git初学者问题:

我有一个小的私有webproject,使用msysgit在本地进行版本控制。没有外部存储库,因为它只适用于我,所以我可以基本上做我想做的任何事情。

我已经在项目目录中设置了这个,即“webroot”。

现在必须创建第二个目录,与webroot并行放置。我们称之为资产。

所以现在结构如下:

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

我希望将这个新目录包含在git存储库中,这样我也会对存储在那里的文件进行版本更改,但当然我不能使用“git add ../assets”。我也不倾向于在project_directory中创建一个新的git项目,因为这会丢失我以前的所有提交。

那么如何将存储库从“webroot”移到“project_directory”中,同时保留我的提交然后能够包含“资产”?

5 个答案:

答案 0 :(得分:63)

所以,你希望你的git repo看起来像这样:

<projectdir>
    /.git
    /webroot
    /assets

为此,您必须将repo 中的现有文件移动到新的webroot子目录中。

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

然后,在您的本地文件系统上,您希望将克隆重定位到目前所在的目录之上:

cd <projectdir>
mv webroot/* .
rmdir webroot

然后你想将assets目录(和文件)添加到git repo:

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

答案 1 :(得分:11)

您也可以将.git dir上移一级并更新您的工作树。

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

不是正面的,但我很确定core.worktree的路径必须是绝对的。

答案 2 :(得分:9)

我假设您打算重写历史记录以包含所有修订版中的所有文件,就好像它们一直位于webroot /的子目录中而不是根目录

git filter-branch联机帮助页有答案,这里是一个改进的版本,它重写了所有现有的引用(分支)和标记:

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

已经注意使其成为仅限索引的操作,以便即使对于大回购也可以快速运行。记得(当满意时)摆脱原始引用(.git / refs / original / *)并重新打包repo以松开过时的树对象。

答案 3 :(得分:5)

您的提交不会与存储在git repo中的“webroot”文件夹本地绑定。

你可以简单地删除webroot目录,重新检查新位置“/ project directory”中的存储库添加资产目录并提交。

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

答案 4 :(得分:0)

以下命令会重写您的Git历史记录。看起来好像内容一直在webroot中。如果多人正在使用回购,通常重写历史很麻烦。既然你一个人在上面工作,那应该没事。

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'