对Git子模块进行非子模块化,将所有代码带回核心存储库的最佳实践是什么?
当前回答
下面是@gyim的回答的一个稍微改进的版本(IMHO)。他在主工作副本中做了很多危险的改变,我认为在独立的克隆上操作,然后在最后将它们合并在一起要容易得多。
在一个单独的目录中(使错误更容易清理并再次尝试)检出顶部回购和子回购。
git clone ../main_repo main.tmp
git clone ../main_repo/sub_repo sub.tmp
首先编辑subrepo以将所有文件移动到所需的子目录中
cd sub.tmp
mkdir sub_repo_path
git mv `ls | grep -v sub_repo_path` sub_repo_path/
git commit -m "Moved entire subrepo into sub_repo_path"
注意头部
SUBREPO_HEAD=`git reflog | awk '{ print $1; exit; }'`
现在从主回购中删除子回购
cd ../main.tmp
rmdir sub_repo_path
vi .gitmodules # remove config for submodule
git add -A
git commit -m "Removed submodule sub_repo_path in preparation for merge"
最后,合并它们
git fetch ../sub.tmp
# remove --allow-unrelated-histories if using git older than 2.9.0
git merge --allow-unrelated-histories $SUBREPO_HEAD
并完成了!安全而且没有任何魔法。
其他回答
我找到的最佳答案是:
http://x3ro.de/2013/09/01/Integrating-a-submodule-into-the-parent-repository.html
这篇文章很好地解释了这个过程。
对我们来说,我们为两个项目创建了两个存储库,它们是如此耦合,以至于将它们分开没有任何意义,所以我们合并了它们。
我将首先展示如何合并每个主分支,然后解释如何将其扩展到你得到的每个分支,希望它能帮助到你。
如果你让子模块工作,你想把它转换成一个目录,你可以这样做:
git clone project_uri project_name
这里我们做了一个干净的克隆来工作。对于这个过程,您不需要初始化或更新子模块,所以跳过它。
cd project_name
vim .gitmodules
使用您喜欢的编辑器(或Vim)编辑.gitmodules以删除您计划替换的子模块。你需要删除的线条应该看起来像这样:
[submodule "lib/asi-http-request"]
path = lib/asi-http-request
url = https://github.com/pokeb/asi-http-request.git
保存文件后,
git rm --cached directory_of_submodule
git commit -am "Removed submodule_name as submodule"
rm -rf directory_of_submodule
在这里,我们完全删除了子模块关系,这样我们就可以就地创建将另一个repo带入项目。
git remote add -f submodule_origin submodule_uri
git fetch submodel_origin/master
这里我们获取要合并的子模块存储库。
git merge -s ours --no-commit submodule_origin/master
这里我们开始两个存储库的合并操作,但在提交之前停止。
git read-tree --prefix=directory_of_submodule/ -u submodule_origin/master
在这里,我们将子模块中的master内容发送到添加目录名前缀之前的目录
git commit -am "submodule_name is now part of main project"
在这里,我们完成了在合并中提交更改的过程。
完成此操作后,您可以推送,并重新开始使用任何其他分支来合并,只需签出存储库中将接收更改的分支,并更改您在合并和读取树操作中引入的分支。
Git rm——缓存了the_submodule_path 从.gitmodules文件中删除子模块部分,或者如果它是唯一的子模块,则删除该文件。 执行一个提交"removed submodule xyz" Git添加了_submodule_path 另一次提交“添加xyz的代码库”
我还没找到更简单的方法。你可以通过git commit -a把3-5压缩到一个步骤中——这取决于你的喜好。
基于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 log——follow <file>问题的困扰。它也是一个非常简单的单行调用,可以为您完成所有工作。G 'luck。
它建立在Lucas Jenß的出色工作之上,在他的博客文章“将子模块集成到父存储库中”中描述了这一点,但是它自动化了整个过程,并清理了一些其他的极端情况。
最新的代码将在github上的https://github.com/jeremysears/scripts/blob/master/bin/git-submodule-rewrite上进行错误修复,但为了适当的stackoverflow回答协议,我在下面包含了完整的解决方案。
用法:
$ git-submodule-rewrite <submodule-name>
git-submodule-rewrite:
#!/usr/bin/env bash
# This script builds on the excellent work by Lucas Jenß, described in his blog
# post "Integrating a submodule into the parent repository", but automates the
# entire process and cleans up a few other corner cases.
# https://x3ro.de/2013/09/01/Integrating-a-submodule-into-the-parent-repository.html
function usage() {
echo "Merge a submodule into a repo, retaining file history."
echo "Usage: $0 <submodule-name>"
echo ""
echo "options:"
echo " -h, --help Print this message"
echo " -v, --verbose Display verbose output"
}
function abort {
echo "$(tput setaf 1)$1$(tput sgr0)"
exit 1
}
function request_confirmation {
read -p "$(tput setaf 4)$1 (y/n) $(tput sgr0)"
[ "$REPLY" == "y" ] || abort "Aborted!"
}
function warn() {
cat << EOF
This script will convert your "${sub}" git submodule into
a simple subdirectory in the parent repository while retaining all
contents and file history.
The script will:
* delete the ${sub} submodule configuration from .gitmodules and
.git/config and commit it.
* rewrite the entire history of the ${sub} submodule so that all
paths are prefixed by ${path}.
This ensures that git log will correctly follow the original file
history.
* merge the submodule into its parent repository and commit it.
NOTE: This script might completely garble your repository, so PLEASE apply
this only to a fresh clone of the repository where it does not matter if
the repo is destroyed. It would be wise to keep a backup clone of your
repository, so that you can reconstitute it if need be. You have been
warned. Use at your own risk.
EOF
request_confirmation "Do you want to proceed?"
}
function git_version_lte() {
OP_VERSION=$(printf "%03d%03d%03d%03d" $(echo "$1" | tr '.' '\n' | head -n 4))
GIT_VERSION=$(git version)
GIT_VERSION=$(printf "%03d%03d%03d%03d" $(echo "${GIT_VERSION#git version}" | tr '.' '\n' | head -n 4))
echo -e "${GIT_VERSION}\n${OP_VERSION}" | sort | head -n1
[ ${OP_VERSION} -le ${GIT_VERSION} ]
}
function main() {
warn
if [ "${verbose}" == "true" ]; then
set -x
fi
# Remove submodule and commit
git config -f .gitmodules --remove-section "submodule.${sub}"
if git config -f .git/config --get "submodule.${sub}.url"; then
git config -f .git/config --remove-section "submodule.${sub}"
fi
rm -rf "${path}"
git add -A .
git commit -m "Remove submodule ${sub}"
rm -rf ".git/modules/${sub}"
# Rewrite submodule history
local tmpdir="$(mktemp -d -t submodule-rewrite-XXXXXX)"
git clone "${url}" "${tmpdir}"
pushd "${tmpdir}"
local tab="$(printf '\t')"
local filter="git ls-files -s | sed \"s/${tab}/${tab}${path}\//\" | GIT_INDEX_FILE=\${GIT_INDEX_FILE}.new git update-index --index-info && mv \${GIT_INDEX_FILE}.new \${GIT_INDEX_FILE}"
git filter-branch --index-filter "${filter}" HEAD
popd
# Merge in rewritten submodule history
git remote add "${sub}" "${tmpdir}"
git fetch "${sub}"
if git_version_lte 2.8.4
then
# Previous to git 2.9.0 the parameter would yield an error
ALLOW_UNRELATED_HISTORIES=""
else
# From git 2.9.0 this parameter is required
ALLOW_UNRELATED_HISTORIES="--allow-unrelated-histories"
fi
git merge -s ours --no-commit ${ALLOW_UNRELATED_HISTORIES} "${sub}/master"
rm -rf tmpdir
# Add submodule content
git clone "${url}" "${path}"
rm -rf "${path}/.git"
git add "${path}"
git commit -m "Merge submodule contents for ${sub}"
git config -f .git/config --remove-section "remote.${sub}"
set +x
echo "$(tput setaf 2)Submodule merge complete. Push changes after review.$(tput sgr0)"
}
set -euo pipefail
declare verbose=false
while [ $# -gt 0 ]; do
case "$1" in
(-h|--help)
usage
exit 0
;;
(-v|--verbose)
verbose=true
;;
(*)
break
;;
esac
shift
done
declare sub="${1:-}"
if [ -z "${sub}" ]; then
>&2 echo "Error: No submodule specified"
usage
exit 1
fi
shift
if [ -n "${1:-}" ]; then
>&2 echo "Error: Unknown option: ${1:-}"
usage
exit 1
fi
if ! [ -d ".git" ]; then
>&2 echo "Error: No git repository found. Must be run from the root of a git repository"
usage
exit 1
fi
declare path="$(git config -f .gitmodules --get "submodule.${sub}.path")"
declare url="$(git config -f .gitmodules --get "submodule.${sub}.url")"
if [ -z "${path}" ]; then
>&2 echo "Error: Submodule not found: ${sub}"
usage
exit 1
fi
if ! [ -d "${path}" ]; then
>&2 echo "Error: Submodule path not found: ${path}"
usage
exit 1
fi
main
推荐文章
- Git rebase—即使所有合并冲突都已解决,仍然会继续报错
- 在Git中,我如何知道我的当前版本是什么?
- 跟踪所有远程git分支作为本地分支
- 自定义SSH端口上的Git
- git如何显示不存在于.gitignore中的未跟踪文件
- Git错误:遇到7个文件应该是指针,但不是
- GitHub克隆与OAuth访问令牌
- 移动(或“撤销”)最后一个git提交到非暂存区域
- 我可以在GitHub上对要点进行拉请求吗?
- Hg:如何做一个像git的rebase
- 如何丢弃远程更改并将文件标记为“已解决”?
- 如何查看远程标签?
- Git:在推送后删除提交的文件
- Git分支之间的视觉差异
- 在GitHub中编辑git提交消息