您可以更改.gitmodules中的路徑,然後執行git submodule init && git submodule update
,也可以將子模塊轉換爲子樹。最後一個選項涉及某種腳本,因爲沒有自動執行此操作的方法。
當使用shell腳本,你可以使用這些函數來獲得子模塊和他們的名字(從混帳子模塊源):
# This lists the unmerged submodules
module_list()
{
git ls-files --error-unmatch --stage -- "[email protected]" |
perl -e '
my %unmerged =();
my ($null_sha1) = ("0" x 40);
while (<STDIN>) {
chomp;
my ($mode, $sha1, $stage, $path) =
/^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
next unless $mode eq "160000";
if ($stage ne "0") {
if (!$unmerged{$path}++) {
print "$mode $null_sha1 U\t$path\n";
}
next;
}
print "$_\n";
}
'
}
# This gets the submodule's name from the .gitignore file in your webroot
module_name()
{
# Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
name=$(git config -f $2/.gitmodules --get-regexp '^submodule\..*\.path$' |
sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p')
test -z "$name" &&
die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$path'")"
echo "$name"
}
然後,當你重複的結果,可以提取名稱和url用於子樹。
module_list | while read mode sha1 stage path
do
#the path as your repo sees it
full_path=$path
#The path as .gitmodules in your child_directory sees it
fake_path=${path[*]/[child_directory]\//}
#Get the name of the submodule
stupid_name=$(module_name "$fake_path" "[child_directory]")
#Get the git URL from the .gitmodules file
git_url=$(git config -f [child_directory]/.gitmodules submodule."$stupid_name".url)
#Gets the clean name to use as remote name
new_remote=$(echo $git_url | cut -d. -f2 | cut -d/ -f2- | cut -d/ -f2)
#Remove the submodules folder
rm -rf $path
#Add the subtree
git remote add -f $new_remote $git_url
git merge -s ours --no-commit $new_remote/master
git read-tree --prefix=$full_path/ -u $new_remote/master
git commit -m "$new_remote merged in $full_path"
done
我敢肯定有一個更好的方式這樣做的方式,但它給人的是什麼做
子樹合併的使用幾乎讓你越界「自動執行」。只需編輯路徑。 – 2011-03-25 06:13:46