git

2020-01-26-git-git子模块克隆-拉取-推送-删除

我想念泉州的夏天了

git子模块的一些操作

克隆一个子模块

clonesubmodule.sh

#!/bin/bash

path=ssh://git@your_git/path/to/$1
git submodule add $path $1
if [ $? -ne 0 ]
then
  echo 
  echo "clone submodule fail!"
fi

./clonesubmodule.sh dir/project

将远程的dir/project项目拷贝到当前目录的dir/project目录下

拉取所有子模块

pullsubmodule.sh 利用git submodule foreach 这个命令,遍历所有子模块,然后再对每次遍历使用 git fetch 和git merge合并仓库。(git submodule foreach挺强大的,我们可以在里面写多条git语句)

#!/bin/bash

git submodule foreach '
git fetch;
git merge;
'
./pullsubmodule.sh

拉取当前目录下所有子模块的更新

推送子模块

可以根据需要修改,推送单个子模块

#!/bin/bash

git submodule foreach '
git add .;
git commit -m "diary update"; 
git push origin master;
'

if [ $? -ne 0 ]
then
  echo 
  echo "push submodule fail!"
fi

删除子模块

删除某个子模块

#!/bin/bash
# 2019-07-26 V1.01
# 移除子模块,示例:移除C-code/testcode的代码
# ./deletesubmodule.sh C-code/testcode

if [ -z "$1" ]; then 
    echo "ERROR:please input delete path"
    exit 1
fi

path=$1
read -p " INFO:are you sure delete $path?(yes/no)" answer

case $answer in
y | Y | yes | YES)
    # 删除
    ;;
*)
    # 不删除
    echo " INFO:stop delete, quit"
    exit 0
    ;;
esac


rm -rf .git/modules/$path
if [ 0 -ne $? ]; then
    echo "ERROR:rm -rf .git/modules/$path"
    exit 1
fi

git rm -r --cache $path
if [ 0 -ne $? ]; then
    echo "ERROR:git rm -r --cache $path fail"
    exit 1
fi

rm -rf $path
if [ 0 -ne $? ]; then
    echo "ERROR:rm -rf $path"
    exit 1
fi


echo " INFO:delete ok"

# 需要手动删除
echo "please edit .gitmodules and remove lines relative to $path"
echo "please edit .git/config and remove lines relative to $path"

删除子模块后,需要手动修改两个文件

# 删除对应要删除的子模块的那三行
vi .gitmodules

# 删除对应要删除的子模块的那三行
vi .git/config