git submodules

Git submodules使用方法

1.构建submodule实例

Step1:下载git父仓库
cd /root/workspace
git clone https://github.com/daneren/main_project.git
Step2:添加git子仓库
cd /root/workspace/main_project
git submodule add https://github.com/daneren/sub_project.git
Step3:初始化main_project
git submodule update --init --recursive
Step3:查看状态
cd /root/workspace/main_project

git status
# On branch main
# Your branch is up to date with 'origin/main'.

# Changes to be committed:
#  (use "git restore --staged <file>..." to unstage)
#         new file:   .gitmodules
#         new file:   sub_project
       
 
# 进入sub_project
cd /root/workspace/main_project/sub_project

git status
# On branch main
# Your branch is up to date with 'origin/main'.

# nothing to commit, working tree clean

git branch -a
# * (HEAD detached at origin/main)
#   main
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/main
  
# sub_project从流离状态切换main分支
git checkout main
# Switched to branch 'main'
# Your branch is up to date with 'origin/main'.

git branch -a
# * main
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/main
Step4:更新主仓库
cd /root/workspace/main_project
git commit -am 'add submodule sub_project'
git push

image-20220303143253190

2.下载submodule实例

直接下载克隆含有子模块的项目
git clone https://github.com/daneren/main_project.git
cd main_project/
git submodule update --init --recursive # 初始化所有子模块

or

git clone --recurse-submodules https://github.com/daneren/main_project.git #自动初始化main_project包含的所有submodule

3.修改sub_project并在main_project同步

修改sub_project
cd /root/workspace/main_project/sub_project

echo "This is a submodule." > new.txt

git status
# HEAD detached from refs/heads/main
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#         new.txt

# nothing added to commit but untracked files present (use "git add" to track)

git branch -a
# * (HEAD detached from refs/heads/main)
#   main
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/main

git add .
git commit -m 'add new.txt' 
git push origin HEAD:main # 提交到主分支

image-20220303152818558

main_project同步
cd /root/workspace/main_project/

git status
# On branch main
# Your branch is up to date with 'origin/main'.

# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git restore <file>..." to discard changes in working directory)
#         modified:   sub_project (new commits)

# no changes added to commit (use "git add" and/or "git commit -a")

git add .
git commit -m "update submodule add new.txt"
git push

image-20220303153154469

单独下载sub_project修改并提交,在原有main_project更新到最新sub_project remote commit
cd /root/workspace/
git clone git@github.com:daneren/sub_project.git
cd sub_project

echo "This is a new2 file." > new2.txt

git add . && git commit -m 'add new2.txt'

git push

image-20220303153733660

# init main_project
rm -rf /root/workspace/main_project
# update main_project
cd /root/workspace/
git clone https://github.com/daneren/main_project.git
cd /root/workspace/main_project

git submodule update --init --recursive
# Submodule 'sub_project' (https://github.com/daneren/sub_project.git) registered for path 'sub_project'
# Cloning into '/data/workspace/main_project/sub_project'...
# Submodule path 'sub_project': checked out 'ceee669bd8e29bb3cf2c049af0b4b7613208f952'

# 选择sub_projrct branch 拉取最新代码
git pull origin HEAD:main

git add .
git commit -m 'update submodule add new2.txt'
git push

image-20220303164240835

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值