简体   繁体   English

更改 Git 存储库的根目录

[英]Change the root directory of a Git repository

I've got the following source structure:我有以下源结构:

/dir1
    file1
    file2
    file3

dir1 is unneeded as the repository itself can be like a folder, so I want my git repository to look like this:不需要dir1 ,因为存储库本身可以像一个文件夹,所以我希望我的 git 存储库看起来像这样:

file1
file2
file3

What should I do to achieve this?我应该怎么做才能实现这一目标?

DISCLAIMER: this will rewrite history and be a PITA for anyone who has already cloned your repository.免责声明:这将改写历史并成为任何已经克隆您的存储库的人的 PITA。 You shouldn't do it on published history.你不应该在已发表的历史上这样做。

That said, you should be able to rewrite all your trees by using the filter-branch command of Git.也就是说,您应该能够使用 Git 的filter-branch命令重写所有树。 Be sure to understand all implications before using it (please, read the manpage; have backups).在使用之前一定要了解所有的含义(请阅读联机帮助页;有备份)。

git filter-branch \
  --subdirectory-filter dir1 \
  --tag-name-filter cat \
  -- --all

NB.注意。 This command will also perist your grafts and replace refs.此命令还将保留您的移植并替换参考。

You can move your .git in the subdirectory (don't forget your .gitignore).您可以将 .git 移动到子目录中(不要忘记您的 .gitignore)。 Then run git init :然后运行 ​​git init :

Important : Do a last commit before moving (i'll explain it...)重要:在移动之前做最后一次提交(我会解释它......)

mv .git ./dir1/.
git init .
git add .
git commit

It's important to do a last commit before moving : git uses the hash of files to recognize them.在移动之前做最后一次提交很重要:git 使用文件的哈希来识别它们。 unchanged files are seen as 'renamed'.未更改的文件被视为“重命名”。 If you have uncomitted files before moving, it will log them a couple 'deleted' 'added' for each modified file and you'll loose history on those.如果您在移动之前有未提交的文件,它会为每个修改过的文件记录一些“删除”“添加”,并且您将丢失这些文件的历史记录。

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

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