对Git子模块进行非子模块化,将所有代码带回核心存储库的最佳实践是什么?
当前回答
以下是我发现的最好最简单的方法。
在子模块repo中,你想从HEAD合并到main repo:
git checkout -b "mergeMe" mkdir "foo/bar/myLib/"(与你想要在主repo上的文件的路径相同) git mv * "foo/bar/myLib/"(移动所有到路径) Git commit -m“准备合并到main”
在删除子模块并清除路径"foo/bar/myLib"后,回到主repo:
SubmoduleOriginRemote/mergeMe
繁荣做
记录保存
不用担心
注意,这与其他一些答案几乎相同。但这假设你拥有子模块repo。此外,这也使子模块的未来上游更改变得容易。
其他回答
如果你只是想把你的子模块代码放到主库中,你只需要删除子模块,并将文件重新添加到主库中:
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"
注意,这并不能保存历史。
我发现从子模块获取本地提交数据更方便,否则我就会丢失它们。(无法推送,因为我没有遥控器)。所以我添加了submodule/。Git为remote_origin2,从该分支中提交并合并。 不确定我是否仍然需要子模块远程作为起源,因为我对git还不够熟悉。
基于VonC的回答,我创建了一个简单的bash脚本。最后的add必须使用通配符,否则它将撤销子模块本身先前的rm。重要的是要添加子模块目录的内容,而不是在add命令中命名目录本身。
在一个名为git- integration -submodule的文件中:
#!/usr/bin/env bash
mv "$1" "${1}_"
git submodule deinit "$1"
git rm "$1"
mv "${1}_" "$1"
git add "$1/**"
在主回购
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添加?
- 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之间的区别