如何将git(集线器)存储库切换到上一级?
我有一个叫做foo的项目。在我的计算机上,此项目作为名为foo_container的非git文件夹的子文件夹驻留,即位于foo_container/foo中。foo是一个git文件夹,我还将它同步到github,并做了一些更改 后来,我在foo_container下的其他文件夹中添加了更多内容,比如foo_container\foo_docs,并希望从现在起将foo_container作为我的repo签入。我不想丢失foo的git历史记录,但我当然同意从现在开始只记录foo_容器的更改 最干净的方法是什么?以下是我考虑过的几个选项: 备选案文1:如何将git(集线器)存储库切换到上一级?,git,github,Git,Github,我有一个叫做foo的项目。在我的计算机上,此项目作为名为foo_container的非git文件夹的子文件夹驻留,即位于foo_container/foo中。foo是一个git文件夹,我还将它同步到github,并做了一些更改 后来,我在foo_container下的其他文件夹中添加了更多内容,比如foo_container\foo_docs,并希望从现在起将foo_container作为我的repo签入。我不想丢失foo的git历史记录,但我当然同意从现在开始只记录foo_容器的更改 最干净的
cd /path/to/foo_container
git init
<rename repository on github from foo to foo_container>
<some git-remote command - I don't know what this should be to switch existing repo>
选项2可能有效,但我怀疑我会丢失foo目录的git历史记录
删除github上的旧foo repo,创建一个名为foo_container的新repo、foo_container中的git init和git remote add origin
我不确定这两种方法是否有效,以及哪种方法更好。那么
cd foo_container/foo
mkdir foo
git mv * foo/
git commit -m "Move foo to subfolder"
这会让你有一个
foo_container/
foo/
.git/ (git repo data)
foo/ (actual contents you've been tracking)
bar/ (other thing you want to add)
foo_container/
.git/ (git repo data)
foo/ (actual contents you were tracking)
bar/ (other thing you want to add)
然后,将当前在foo_container/foo中的所有内容直接移动到foo_container中,包括.git目录,留下一个
foo_container/
foo/
.git/ (git repo data)
foo/ (actual contents you've been tracking)
bar/ (other thing you want to add)
foo_container/
.git/ (git repo data)
foo/ (actual contents you were tracking)
bar/ (other thing you want to add)
现在,您在foo_容器中拥有的所有非foo的内容都将显示为新文件,添加到您的git存储库中,该存储库现在位于foo_容器中,而foo仍将显示为文件夹,并且由于git mv,您在旧提交中仍然拥有历史记录
git add bar
git commit -m "Add bar"
git push origin HEAD
那么
cd foo_container/foo
mkdir foo
git mv * foo/
git commit -m "Move foo to subfolder"
这会让你有一个
foo_container/
foo/
.git/ (git repo data)
foo/ (actual contents you've been tracking)
bar/ (other thing you want to add)
foo_container/
.git/ (git repo data)
foo/ (actual contents you were tracking)
bar/ (other thing you want to add)
然后,将当前在foo_container/foo中的所有内容直接移动到foo_container中,包括.git目录,留下一个
foo_container/
foo/
.git/ (git repo data)
foo/ (actual contents you've been tracking)
bar/ (other thing you want to add)
foo_container/
.git/ (git repo data)
foo/ (actual contents you were tracking)
bar/ (other thing you want to add)
现在,您在foo_容器中拥有的所有非foo的内容都将显示为新文件,添加到您的git存储库中,该存储库现在位于foo_容器中,而foo仍将显示为文件夹,并且由于git mv,您在旧提交中仍然拥有历史记录
git add bar
git commit -m "Add bar"
git push origin HEAD
第一种方法很好,因为可以将foo声明为foo_容器的子模块 但最简单的方法是: 将本地foo重命名为foo_容器,以便foo_容器/foo_容器 git mv foo_container/foo_container在新文件夹“foo”foo_container/foo_container/foo下的内容 在foo_container/foo_container/foo_docs中复制foo_container\foo_文档:添加、提交和推送 将GitHub foo repo重命名为foo_容器 用foo_容器/foo_容器替换本地foo_容器。
第一种方法很好,因为可以将foo声明为foo_容器的子模块 但最简单的方法是: 将本地foo重命名为foo_容器,以便foo_容器/foo_容器 git mv foo_container/foo_container在新文件夹“foo”foo_container/foo_container/foo下的内容 在foo_container/foo_container/foo_docs中复制foo_container\foo_文档:添加、提交和推送 将GitHub foo repo重命名为foo_容器 用foo_容器/foo_容器替换本地foo_容器。 将foo中的所有内容移动到名为foo的子目录中,这样您就可以将foo/foo作为实际的项目。采取这一行动。 将所有内容从foo_容器移动到foo中,这样您将拥有foo/foo_文档之类的内容。添加并提交所有这些。 使foo成为顶级foo_容器目录,并删除旧目录。i、 e.在foo_容器中时,执行mv foo../foo_容器2、cd..rmdir foo_容器、mv foo_容器2 foo_容器。 将foo中的所有内容移动到名为foo的子目录中,这样您就可以将foo/foo作为实际的项目。采取这一行动。 将所有内容从foo_容器移动到foo中,这样您将拥有foo/foo_文档之类的内容。添加并提交所有这些。 使foo成为顶级foo_容器目录,并删除旧目录。i、 e.在foo_容器中时,执行mv foo../foo_容器2、cd..rmdir foo_容器、mv foo_容器2 foo_容器。
通过使用重写历史记录,可以避免出现“然后所有内容都被移动并提交”的情况:
cd /path/to/foo_container/foo
git filter-branch --tree-filter 'mkdir foo; git mv -k * foo/ ' HEAD
cd /path/to/foo_container
mv foo/* foo/.git . # and any other hidden files, like .gitignore
git add foo_docs
现在,您的目录如下所示:
foo_container
├── .git
└── foo
| └── stuff
└── foo_docs
└── other stuff
你的历史似乎一直都是这样
注意:重写历史之后,您必须在下一个git push命令中添加-force
注意:在您使用git push-force之前,您应该检查git日志,并确保没有发生灾难
注意:正如努法尔·易卜拉欣(Noufal Ibrahim)在下面提到的那样,重写历史会给任何拉旧历史的人带来额外的工作。您可以通过使用重写历史来避免使用“然后所有内容都被移动”提交:
cd /path/to/foo_container/foo
git filter-branch --tree-filter 'mkdir foo; git mv -k * foo/ ' HEAD
cd /path/to/foo_container
mv foo/* foo/.git . # and any other hidden files, like .gitignore
git add foo_docs
现在,您的目录如下所示:
foo_container
├── .git
└── foo
| └── stuff
└── foo_docs
└── other stuff
你的历史似乎一直都是这样
注意:重写历史之后,您必须在下一个git push命令中添加-force
注意:在您使用git push-force之前,您应该检查git日志,并确保没有发生灾难
注:正如努法尔·易卜拉欣(Noufal Ibrahim)在下面提到的那样,改写历史将给任何翻过旧历史的人带来额外的工作。3
我告诉你做同样的事。这是你们的共识是的,谢谢!同一答案上的所有细微变化,都接受了最详细的答案。3个答案都告诉你做同样的事情。这是你们的共识是的,谢谢!相同答案上的所有细微变化,因此接受了最详细的答案。但是,如果您的存储库是公共的,即在多个用户之间共享,则重写历史是不可行的。但是,如果您的存储库是公共的,即在多个用户之间共享,则重写历史是不可行的。