对Git子模块进行非子模块化,将所有代码带回核心存储库的最佳实践是什么?
当前回答
我发现从子模块获取本地提交数据更方便,否则我就会丢失它们。(无法推送,因为我没有遥控器)。所以我添加了submodule/。Git为remote_origin2,从该分支中提交并合并。 不确定我是否仍然需要子模块远程作为起源,因为我对git还不够熟悉。
其他回答
这里有很多答案,但它们似乎都过于复杂,可能不能达到你想要的效果。我相信大多数人都想保留他们的历史。
在这个例子中,主repo将是git@site.com:main/main.git,子模块repo将是git@site.com:main/child.git。这假设子模块位于父repo的根目录中。根据需要调整说明书。
首先克隆父repo并删除旧的子模块。
git clone git@site.com:main/main.git
git submodule deinit child
git rm child
git add --all
git commit -m "remove child submodule"
现在我们将把子回购添加到主回购的上游。
git remote add upstream git@site.com:main/child.git
git fetch upstream
git checkout -b merge-prep upstream/master
下一步假设您希望将merge-prep分支上的文件移动到与上面的子模块相同的位置,尽管您可以通过更改文件路径轻松更改位置。
mkdir child
将除.git文件夹外的所有文件夹和文件移动到子文件夹中。
git add --all
git commit -m "merge prep"
现在您可以简单地将文件合并回主分支。
git checkout master
git merge merge-prep # --allow-unrelated-histories merge-prep flag may be required
在运行git push之前,环顾四周,确保一切看起来都很好
你现在必须记住的一件事是,git日志默认情况下不会跟踪移动的文件,但是通过运行git log——follow filename,你可以看到你文件的完整历史。
我找到的最佳答案是:
http://x3ro.de/2013/09/01/Integrating-a-submodule-into-the-parent-repository.html
这篇文章很好地解释了这个过程。
在主回购
Git rm——缓存[submodules_repo] git commit -m "Submodules removed." Git push origin [master]
在子模块repo
Rm -rf .git
还是主回购
Git add [submodules_repo] Git添加。 Submodules repo被添加到main中。 Git push origin [master]
如果你只是想把你的子模块代码放到主库中,你只需要删除子模块,并将文件重新添加到主库中:
git rm --cached submodule_path # delete reference to submodule HEAD (no trailing slash)
git rm .gitmodules # if you have more than one submodules,
# you need to edit this file instead of deleting!
rm -rf submodule_path/.git # make sure you have backup!!
git add submodule_path # will add files instead of commit reference
git commit -m "remove submodule"
如果还想保留子模块的历史,可以使用一个小技巧:将子模块“合并”到主存储库中,这样结果将与以前相同,只是子模块文件现在位于主存储库中。
在主模块中,你需要做以下工作:
# Fetch the submodule commits into the main repository
git remote add submodule_origin git://url/to/submodule/origin
git fetch submodule_origin
# Start a fake merge (won't change any files, won't commit anything)
git merge -s ours --no-commit submodule_origin/master
# Do the same as in the first solution
git rm --cached submodule_path # delete reference to submodule HEAD
git rm .gitmodules # if you have more than one submodules,
# you need to edit this file instead of deleting!
rm -rf submodule_path/.git # make sure you have backup!!
git add submodule_path # will add files instead of commit reference
# Commit and cleanup
git commit -m "removed submodule"
git remote rm submodule_origin
生成的存储库看起来有点奇怪:将会有多个初始提交。但是它不会给Git带来任何问题。
第二种解决方案的一大优点是,您仍然可以在最初位于子模块中的文件上运行git blame或git log。实际上,这里所发生的只是在一个存储库中重命名了许多文件,Git应该自动检测到这一点。如果你仍然有git日志的问题,尝试一些选项(例如,——follow, -M, -C),它们可以更好地重命名和复制检测。
当
git rm [-r] --cached submodule_path
返回
fatal: pathspec 'emr/normalizers/' did not match any files
背景:我在子模块文件夹中执行了rm -r .git*,然后才意识到它们需要在我刚刚添加它们的主项目中去子模块化。我得到了上面的错误去子模化一些,但不是所有的。不管怎样,我通过运行(当然,在rm -r .git*之后)来修复它们。
mv submodule_path submodule_path.temp
git add -A .
git commit -m "De-submodulization phase 1/2"
mv submodule_path.temp submodule_path
git add -A .
git commit -m "De-submodulization phase 2/2"
注意,这并不能保存历史。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别