[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"

 

posted @   Zhentiw  阅读(541)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 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工具
点击右上角即可分享
微信分享提示