2

After I git submodule update it always checks out the same commit. for example 34561.

I do git checkout master for submodule and then git submodule sync. Then it points to the latest commit a2344.

But after update it again points to the commit 34561.

How to change it? I mean why it decided to point to that commit and not another?

Martin G
  • 17,357
  • 9
  • 82
  • 98
lapots
  • 12,553
  • 32
  • 121
  • 242

4 Answers4

3

I mean why it decided to point to that commit and not another?

Because a submodule always records a fixed SHA1 commit in the parent repo as a gitlink (a special entry in the index).
That is why a submodule is always restored as a detached HEAD branch

You can configure a submodule to follow a branch

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

The submodule would still be restored to a fixed commit, but can then be updated with:

git submodule update --remote

Make sure to add and commit the new gitlink in the parent repo (since updating a submodule to the latest of a branch would change its SHA1, recorded in the parent repo as a gitlink).
If you don't, you will find back your submodule to its previous state at then next git submodule update --init.

See more at "Git submodules: Specify a branch/tag".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

The 'main' project contains a reference to the SHA-1 of the checked out version each submodule. This is part of the commit in the main project.

If you change the head of a submodule manually in the submodule, you need to tell the main project that from now on it should use this SHA-1 for the submodule.

git commit -a

will do this, as the main project will see the submodules head was updated.

This may be surprising, but it is actually a nice feature. With checking in the head in the main project, you basically tell your colleagues that it is now ok to use the newer version of the submodule. This allows people to work together without too much risk of pulling the rug under others feet.

(That being said, there are many warts in a workflow with submodules, and you probably should agree an approach and the cement it with some team scripts to avoid the pitfalls).

Peter Tillemans
  • 34,983
  • 11
  • 83
  • 114
0

You need to go into the submodule folder and execute:

git fetch --all --prune
git pull origin master

And now you will have the latest commit in the submodole.
Once you run the fetch its updatigng your .git folder under the submodule with the latest commits. It will make sure that you have the recent changes.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • the question is not how to get the latest but why it checks that specific `34561` commiy – lapots May 20 '15 at 07:59
  • I need to see the repository to make sure but it looks like your submodule is not updated so you keep getting the same repository. Can you post the last commit id of your submodule before and after the `git submodule update` – CodeWizard May 20 '15 at 08:03
0

You can execute:

git submodule foreach git pull origin master

This will update all your submodules.

Yordan Ivanov
  • 580
  • 4
  • 11
  • I am doing exactly the same (but just `git submodule foreach git pull`). The point is that why it always updates to the same `34561` commit – lapots May 20 '15 at 07:58
  • 1
    Git stores the SHA1 commit ID into the repository's database. Locking the submodule with a specific commit ID guarantees you your code will work and no breaking changes will be applied with any update. However if you pull the master your repository will lock with the latest commit. – Yordan Ivanov May 20 '15 at 08:05