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
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 # 提交到主分支
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
单独下载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
# 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