Force Git submodules to always stay current

GitGit Submodules

Git Problem Overview


I love git submodules. Also, I hate git submodules. What I love about them is how it enables to you to cleanly compartmentalize dependencies etc. I get the point of having them point to a specific commit on a repo, I do. But in my case, I'm building a library that will be used in another project, so I want to keep it in that seperate repo.

However, the annoyance comes when I'm working on a daily basis on this library and I constantly have to switch back to the app using my library to commit the pointer update.

So, is it possible to have a git submodule just always be on the head of the repo it's pointing at while I'm constantly updating and adding to this library?

Git Solutions


Solution 1 - Git

As I mention in "git submodule tracking latest", you can since git 1.8.2 (March 2013) make a submodule track the HEAD of branch:

git submodule add -b <branch> <repository> [<path>]

A submodule SHA1 is still recorded in the parent repo as a gitlink (special entry in the index)

But a git submodule update --remote will update that entry to the SHA1 matching the HEAD of a branch of the submodule remote repo.

If you have an existing submodule, you can make it follow a branch with:

cd /path/to/your/parent/repo
git config -f .gitmodules submodule.<path>.branch <branch>

cd path/to/your/submodule
git checkout -b branch --track origin/branch
  # if the master branch already exist:
  git branch -u origin/master master

cd /path/to/your/parent/repo
git add path/to/your/submodule
git commit -m "Make submodule tracking a branch"

Solution 2 - Git

UPDATE: As of git 1.8.2, there seems to be a solution. Please see VonC's answer down below. The original answer is left here for the users of git < 1.8.2.


No, and this is by design. If there were a way to point a submodule to the "current head" of some other repository, then it would be impossible to retrieve a historical version (such as a tagged version) from the main repository. It wouldn't know which version of the submodule to check out.

Having said that, you may be interested in the git subtree script. This offers a different way of working with submodules that may be more compatible with your workflow. I was just reminded of this by the recent post on HN.

Solution 3 - Git

Why don't you make changes inside the submodule directory, which itself is a git repo? This way, your app will always have updated library.

Caveats:

  1. You still need to commit the submodule change inside your app repo to put the change in version control (for the app).

  2. If there are more apps than one that are using this library, than this is not going to work, since only one app will be up-to-date at any given time.

Solution 4 - Git

There is no such thing for the moment. To keep the code up-to-date, I use the following commands:

download all for the first time: git clone --recursive http://github.com/<your repo>
download updates in existing repo: git submodule update --remote --recursive --merge

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenView Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - GitGreg HewgillView Answer on Stackoverflow
Solution 3 - GitSaileshView Answer on Stackoverflow
Solution 4 - GitIcebergView Answer on Stackoverflow