what does this git command do?
git submodule update --remote
It updates all the submodules to master branch. You can, of course change this behavior so that it points to another branch.
In the parent git repository, open up a file called .gitmodules inside it will look like this
[submodule “Dependencies/DFP”]
path = Dependencies/DFP
url = https://github.com/wh1pch81n/DFP.git
The command will look in this file for a branch value and if it doesn’t find it it defaults to master. You can change it to point to a different branch by modifying the file like so…
[submodule “Dependencies/DFP”]
path = Dependencies/DFP
url = https://github.com/wh1pch81n/DFP.git
branch = CoolBranch
Now when you run the command it will make DFP point to the branch, “CoolBranch”
** note: Use a single tab, not spaces for each key-value
Thats great! Now I can manually control the version of DFP I am using in my project. The bad news is you can not use a tag or a SHA. Well, At least not without a work around.
git submodule foreach 'git fetch; BRANCH="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; if [[ $BRANCH != "" ]]; then git checkout $BRANCH; fi'
There is quite a bit going on in here so lets break it down.
# A loop that goes through the submodules
git submodule foreach # Get the value of branch from the .gitmodules file
BRANCH="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"# Checkout the branch if BRANCH has a value
if [[ $BRANCH != "" ]]; then
git checkout $BRANCH;
fi
The benefit of this approach is that now we can use tags, SHA, and even branches.
[submodule “Dependencies/DFP”]
path = Dependencies/DFP
url = https://github.com/wh1pch81n/DFP.git
branch = a-tag-1-2-3
This gitmodule seems oddly familiar… It is similar to a cartfile or a podfile
// cartfile
github "wh1pch81n/DFP" "a-tag-1-2-3"
Finally, you may plan to use the script every day. It is not exactly the shortest so it may be best to turn it into an alias for your project.
In your project create a file called “.gitalias” and add the following to the file
# To Install alias into your project:
## git config include.path ../.gitalias[alias]
# make submodules point to branch value specified in .gitmodules
mysu = "submodule foreach 'git fetch; BRANCH=$(git config --file $toplevel/.gitmodules submodule.$name.branch); if [[ $BRANCH != \"\" ]]; then git checkout $BRANCH; fi'"
You will be able to commit this file to your repository and share it with your team. Each team member will need to install it in order to start using the functionality though.
open “.git/config” and add the following:
[include]
path = ../.gitalias
now I can call “git mysu” to run the long custom script!