简体   繁体   English

Git的。 更改存储库根文件夹

[英]Git. Change repository root folder

I have my project in folder C:/projects/NameOfProject/MyCode/ , and .git is located in folder MyCode/ , so only MyCode's contents are in repository. 我的项目位于文件夹C:/projects/NameOfProject/MyCode/.git位于文件夹MyCode/ ,因此只有MyCode的内容位于存储库中。

Recently I added some other files that are in the folder OtherFiles/ related to the project in folder NameOfProject/ . 最近,我补充说,在该文件夹的一些其他文件OtherFiles/关系到文件夹中的项目NameOfProject/ I can't move these to MyCode/ due to relationship between files in MyCode/ and OtherFiles/ . 我不能移动这些到MyCode/由于文件之间的关系MyCode/OtherFiles/ And now I need to change repository settings somehow, the folders MyCode/ and OtherFiles/ will be located at the root of repository. 现在,我需要以某种方式更改存储库设置,文件夹MyCode/OtherFiles/将位于存储库的根目录。 And moreover, it's highly desirable to continue commit history: next commit after changing root of repository will keep on the history of the current branch. 此外,非常需要继续提交历史记录:更改存储库根目录后的下一次提交将保留当前分支的历史记录。

To summarize the above: I have repo in C:/projects/NameOfProject/MyCode/ and I want to move repo root to C:/projects/NameOfProject/ not moving files/subfolders in both of these folders. 总结一下以上内容:我在C:/projects/NameOfProject/MyCode/并且我想将存储库根目录移到C:/projects/NameOfProject/而不要在这两个文件夹中都移动文件/子文件夹。

How can I achieve that? 我该如何实现?

You need to rewrite directory names in the entire NameOfProject/MyCode/ repository using git filter-branch . 您需要使用git filter-branch在整个NameOfProject/MyCode/存储库中重写目录名称。 I recommend to it this way: 我这样推荐:

  1. Backup everything. 备份所有内容。
  2. Copy directory MyCode to a temporary location. 将目录MyCode复制到一个临时位置。
  3. (In that temp dir) rewrite directories with the command: git filter-branch --tree-filter 'mkdir MyCode && mv * MyCode || :' -- --all (在该临时目录中)使用以下命令重写目录: git filter-branch --tree-filter 'mkdir MyCode && mv * MyCode || :' -- --all git filter-branch --tree-filter 'mkdir MyCode && mv * MyCode || :' -- --all . git filter-branch --tree-filter 'mkdir MyCode && mv * MyCode || :' -- --all The filter will rewrite all branches; 过滤器将重写所有分支; for every existing commit it creates a new subdirectory MyCode and moves all files into it (ignoring errors; there will be at least one error — cannot mv MyCode into MyCode ). 对于每个现有的提交,它将创建一个新的子目录MyCode并将所有文件移入其中(忽略错误;将至少存在一个错误-无法将MyCode转换为MyCode )。 This is the main part of the entire process; 这是整个过程的主要部分; it prepares the repository to expect all files in subdirectory MyCode instead of the root of the repository but preserves the commit history. 它准备使存储库期望MyCode子目录中的所有文件而不是存储库的根目录,但保留提交历史记录。
  4. Move the new .git directory to C:/projects/NameOfProject/ (not to MyCode !) and cleanup temp dir. 将新的.git目录移动到C:/projects/NameOfProject/ (而不是MyCode !)并清理temp dir。
  5. Verify you have a backup and remove MyCode/.git . 确认您有备份并删除MyCode/.git
  6. git add OtherFiles
  7. git commit -m OtherFiles

You can initialize a new repo under C:/projects/NameOfProject/ by 您可以在C:/ projects / NameOfProject /下通过以下方式初始化一个新的仓库

git init git初始化

and pull the remote branch under that repo and just copy and replace those two folders MyCode and OtherFiles into initialized git repo and push it to your remote. 并将远程分支拉到该仓库下,只需将MyCode和OtherFiles这两个文件夹复制并替换到初始化的git repo中,然后将其推送到您的远程仓库即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM