[Git] Submodule
SUBMODULE
You're in the process of putting together two websites. One is for the Mythical Wildlife Fund: MWF.com. The other site is an adoption site for stray unicorns called unicornrescues.com. Both sites will share code for a JavaScript image gallery along with the associated css. Let's get started by adding a submodule containing the JavaScript at"git@petstore.com:gallery_js.git"
.
$ git submodule add "git@petstore.com:gallery_js.git" Cloning into 'gallery_js'... done.
STAGING A SUBMODULE
All that's left to do is to commit your submodule so that other collaborators can use it. Using a single git command, stage the current changes to .gitmodules
and gallery_js
.
$ git add --all
COMMITTING A SUBMODULE
Great! Now go ahead and commit the submodule. Don't forget the commit message!
$ git commit -m "Add gallery_js submodule" [master 4adec83] Add gallery_js submodule 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 gallery_js
INIT SUBMODULE
You're helping a co worker get set up to work on unicornrescues.com. You've already cloned the repo. Next, initialize the submodules so they download their own contents.
$ git submodule init Submodule 'gallery_css' (git@petstore.com:gallery_css.git) registered for path 'gallery_css' Submodule 'gallery_js' (git@petstore.com:gallery_js.git) registered for path 'gallery_js' Success!
UPDATE SUBMODULES
Now that you have the submodules initialized, you need to check for updates and make sure they have the correct commits checked out. Run the submodule command that does this.
$ git submodule update Cloning into 'gallery_css'... done. Submodule path 'gallery_css': checked out 'f509481d3935300afd1aaec7a79455a47cc0abba' Cloning into 'gallery_js'... done. Submodule path 'gallery_js': checked out '9f29d1dd5068ed755f6a9a89338e2557ed7c810f' Success!
DETACHED HEAD
You've just committed a few changes to the gallery_css
submodule. When you go to push, you realize that you're in a detached head state. Don't panic, though! We can fix it. Let's start out by creating a new branch with your most recent commit: "a7eded4"
. Name the branchtemp_changes
.
$ git branch temp_changes "a7eded4"
BRANCH MERGE
Your seemingly lost changes now reside on the temp_changes
branch. Go ahead and merge the temp_changes
branch back into master.
$ git merge temp_changes Updating f509481..9043206 Fast-forward gallery.css | 2 ++ unicorn.rb | 13 +++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 unicorn.rb Success!
PUSH CHECK
After finishing up a bunch of changes, you want to push them up to the remote so you can share it with your other co-workers that are working on the project. Since you're using submodules, you should make sure to use the option which checks whether you have un-pushed submodules.
$ git push --recurse-submodules=check Counting objects: 3, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 308 bytes, done. Total 2 (delta 0), reused 0 (delta 0) Success!
ON DEMAND PUSH
We need to push submodule changes again. But this time, instead of going into the submodule to push it, just use the on-demand
option for the --recurse-submodules
option. This way submodules that need it will be pushed automatically.
$ git push --recurse-submodules=on-demand Counting objects: 3, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 308 bytes, done. Total 2 (delta 0), reused 0 (delta 0) Success!
ON DEMAND ALIAS
Wouldn't it be nice if you didn't have to type that long line out every time you wanted to push this project? Let's make the command easier to use. Create an alias which will run the git push --recurse-submodules=on-demand
command. Name the alias anything you want.
$ git config alias.pushall "push --recurse-submodules=on-demand"
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具