Git/Submodules
From charlesreid1
Contents
Notes
I use submodules to organize groups of related repositories and make checking out lots of repos a breeze.
Adding Submodules to Repository
Check out the repository that you want to put submodules into:
git clone https://charlesreid1.com:3000/rpi/pi-master.git cd pi-master
Now add submodules:
git submodule add <git-repo-url>
Here's the script:
#!/bin/bash git submodule add https://charlesreid1.com:3000/rpi/pi-opencv.git git submodule add https://charlesreid1.com:3000/rpi/pi-startup-services.git git submodule add https://charlesreid1.com:3000/rpi/pi-transmission.git git submodule add https://charlesreid1.com:3000/rpi/pi-stunnel.git git submodule add https://charlesreid1.com:3000/rpi/pi-setup.git git submodule add https://charlesreid1.com:3000/rpi/pi-process-wifi-data.git git submodule add https://charlesreid1.com:3000/rpi/pi-join-wifi.git git submodule add https://charlesreid1.com:3000/rpi/pi-aircrack-batch.git
This will add a .gitmodules file automatically.
When you're done, make the commit:
git commit -am 'adding submodules'
Checking Out Repository with Submodules
Start by checking out the repository with the submodules:
git clone https://charlesreid1.com:3000/rpi/pi-master.git
Initialize the repository submodules:
git submodule init
or, do it all at once:
git clone --recursive https://charlesreid1.com:3000/rpi/pi-master.git
or,
git clone --recurse-submodules https://charlesreid1.com:3000/rpi/pi-master.git
https://stackoverflow.com/questions/3796927/how-to-git-clone-including-submodules#4438292
Checking Out Submodules in Existing Repository
If you already have a repository cloned and you just don't have the submodule contents (git won't check out submodule contents by default), you can initialize the contents of the submodules by running:
git submodule update --init
or, if there are multiple folders,
git submodule update --init --recursive
Fetching Changes to Submodules
To pull changes for each of the submodules, run
git submodule update --remote
Dealing with Detached Heads
For whatever reason, git submodules will not automatically take master with them when you make commits directly inside of the submodule directory. So, annoyingly, you run into a problem with commits that you made being on a detached head.
For example, suppose you are doing work on the master branch (commit e1de9), and then you're ready to commit your changes. So you do, resulting in commit a067c89. But git says that commit is a detached head.
Here's what the log looks like:
$ git log --all commit a067c89c6f3d853f42c28e37143978c24c0542f3 (HEAD) Author: Charles Reid <charlesreid1@gmail.com> Date: Mon Mar 12 05:09:43 2018 -0700 moving config files into their own directories, one for each program commit e1de9742b2d669fe74942eb3977b81fb9cd73619 (origin/master, origin/HEAD, master) Author: Charles Reid <charlesreid1@gmail.com> Date: Mon Mar 12 08:14:11 2018 +0000 put local settings in debug mode. commit f2202c34e51b2c5be3b48e0f070fcd100864a86e Author: Charles Reid <charlesreid1@gmail.com> Date: Mon Mar 12 01:37:39 2018 +0000 added LocalSettings.php. Fixing show exceptions value.
How to resolve this hairy situation?
The solution is to turn the head-less commit that you just made into a branch, and then merge that branch into master.
Start by turning the headless commit into a branch:
$ git branch <new-branch-name> a067c89 in this case, I ran: <pre> $ git branch stuff a067c89 <pre> Now we have created a branch stuff with our new changes. To get these changes into master, we just merge the two branches stuff and master: <pre> $ git merge master stuff Updating e1de974..a067c89 Fast-forward
and a git push finishes it off:
$ git push Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (5/5), 1.41 KiB | 1.41 MiB/s, done. Total 5 (delta 0), reused 0 (delta 0) To https://charlesreid1.com:3000/wiki/charlesreid1-config.git e1de974..a067c89 master -> master
WTF Git Why Do You Keep Detaching Heads
I can't figure out why I'm suddenly having this problem.
Git keeps detaching heads. Particularly, when you do a git clone --recursive.
As of 2013, git 1.8, we should be able to track branches, and yet I'm sitting here like a damn caveman trying to update each individual submodule for each individual commit, so that 1 commit turns into 5.
Resources
https://git-scm.com/book/en/v2/Git-Tools-Submodules