亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

將git倉庫從submodule轉換為subtree

標簽:
Git

三个脚本

Alexander Mikhailian

cat .gitmodules |while read i
do
  if [[ $i == \[submodule* ]]; then
    mpath=$(echo $i | cut -d\" -f2)
    read i; read i;
    murl=$(echo $i|cut -d\  -f3)
    mcommit=`eval "git submodule status ${mpath} |cut -d\  -f2"`
    mname=$(basename $mpath)
    echo -e "$name\t$mpath\t$murl\t$mcommit"
    git submodule deinit $mpath
    git rm -r --cached $mpath
    rm -rf $mpath
    git remote add $mname $murl
    git fetch $mname
    git branch _$mname $mcommit
    git read-tree --prefix=$mpath/ -u _$mname
fi
done
git rm .gitmodules

🐾Warning:

下文的两个脚本, 写死了 branch 是 master, 如果主分支不是 master, 需要做相应修改.

Nikita240 - Stack Overflow

📚️Reference:
我对它进行了修改和改进。现在,新的subtree将指向与旧 submodule 相同的提交。以前,脚本只是从目标存储库下载最新的提交,这可能会导致兼容性问题。

#!/bin/bash -x
# This script will convert all your git submodules into git subtrees.
# This script ensures that your new subtrees point to the same commits as the
# old submodules did, unlike most other scripts that do this.
# THIS SCRIPT MUST BE PLACED OUTSIDE OF YOUR REPOSITORY!!!!!!!!!!
# Otherwise, the script will interfere with the git commits.
# Save the script in your home directory as `~/subtrees.sh`
# `cd` into your repository
# Run `~/subtrees.sh`
# Enjoy!

# extract the list of submodules from .gitmodule
cat .gitmodules |while read i
do
if [[ $i == \[submodule* ]]; then
    echo converting $i
    read i
    # extract the module's prefix
    mpath=$(echo $i | grep -E "(\S+)$" -o)
    echo path: $mpath
    read i
    # extract the url of the submodule
    murl=$(echo $i|cut -d\= -f2|xargs)
    echo url: $murl
    # extract the module name
    mname=$(basename $mpath)
    echo name: $mname
    # extract the referenced commit
    mcommit=$(git submodule status $mpath | grep -E "\S+" -o | head -1)
    echo commit: $mcommit
    # deinit the module
    git submodule deinit $mpath
    # remove the module from git
    git rm -r --cached $mpath
    # remove the module from the filesystem
    rm -rf $mpath
    # commit the change
    git commit -m "Removed $mpath submodule at commit $mcommit"
    # add the remote
    git remote add -f $mname $murl
    # add the subtree
    git subtree add --prefix $mpath $mcommit --squash
    # commit any left over uncommited changes
    git commit -a -m "$mname cleaned up"
    # fetch the files
    git fetch $murl master
    echo
fi
done
git rm .gitmodules
git commit -a -m "Removed .gitmodules"

GaspardP - Stack Overflow

📚️Reference:

我稍微修改了一下,调用subtree add而不是read-tree。它将从.gitmodule中获取submodule的列表,并提取模块的前缀、名称和网址。然后它删除每个 submodule,并在同一位置添加它们作为subtree。它还将每个submodule的remote添加为remote,这样你就可以通过提供它的名字而不是它的网址来更新subtree了(即git subtree pull -P Foo Foo master --squash而不是git subtree pull -P Foo https://example.com/foo.git master --squash)。

如果你想把subtree的全部历史导入你的版本库,你可以去掉 --squash 参数。使用 --squash,将只导入subtree的 HEAD 到你的版本库。这可能是大多数人想要的。

#!/bin/bash -x
# extract the list of submodules from .gitmodule
cat .gitmodules |while read i
do
if [[ $i == \[submodule* ]]; then
    echo converting $i

    # extract the module's prefix
    mpath=$(echo $i | cut -d\" -f2)

    # skip two lines
    read i; read i;

    # extract the url of the submodule
    murl=$(echo $i|cut -d\= -f2|xargs)

    # extract the module name
    mname=$(basename $mpath)

    # deinit the module
    git submodule deinit $mpath

    # remove the module from git
    git rm -r --cached $mpath

    # remove the module from the filesystem
    rm -rf $mpath

    # commit the change
    git commit -m "Removed $mpath submodule"

    # add the remote
    git remote add -f $mname $murl

    # add the subtree
    git subtree add --prefix $mpath $mname master --squash

    # fetch the files
    git fetch $murl master
fi
done
git rm .gitmodules

📚️参考文档

三人行, 必有我师; 知识共享, 天下为公. 本文由东风微鸣技术博客 EWhisper.cn 编写.

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
架構師
手記
粉絲
7
獲贊與收藏
22

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消