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