I have a project A which is a library and it is used in a project B.

Both projects A and B have a separate repository on github BUT inside B we have a submodule of A.

I edited some classes on the library, which is in the repo A, I pushed on the remote repo, so the library (repo A) is updated.

These updates do not reflect on the "reference" (the submodule) the submodule refers to a previous commit.... what should I do in order to update the submodule on git?

9

Best Answer


Enter the submodule directory:

cd projB/projA

Pull the repo from you project A (will not update the git status of your parent, project B):

git pull origin master

Go back to the root directory & check update:

cd ..git status

If the submodule updated before, it will show something like below:

# Not currently on any branch.# Changed but not updated:# (use "git add ..." to update what will be committed)# (use "git checkout -- ..." to discard changes in working directory)## modified: projB/projA (new commits)#

Then, commit the update:

git add projB/projAgit commit -m "projA submodule updated"

UPDATE

As @paul pointed out, since git 1.8, we can use

git submodule update --remote --merge

to update the submodule to the latest remote commit. It'll be convenient in most cases.

Since git 1.8 you can do

git submodule update --remote --merge

This will update the submodule to the latest remote commit. You will then need to add and commit the change so the gitlink in the parent repository is updated:

First, git add it

git add project/submodule_proj_name

then git commit it

git commit -m 'gitlink to submodule_proj_name was updated'

the git push it

git push

And then push the changes as without this, the SHA-1 identity the pointing to the submodule won't be updated and so the change won't be visible to anyone else.

If you update a submodule and commit to it, you need to go to the containing, or higher level repo and add the change there.

git status

will show something like:

modified:some/path/to/your/submodule

The fact that the submodule is out of sync can also be seen with

git submodule

the output will show:

+afafaffa232452362634243523 some/path/to/your/submodule

The plus indicates that the your submodule is pointing ahead of where the top repo expects it to point to.

simply add this change:

git add some/path/to/your/submodule

and commit it:

git commit -m "referenced newer version of my submodule"

When you push up your changes, make sure you push up the change in the submodule first and then push the reference change in the outer repo. This way people that update will always be able to successfully run

git submodule update

More info on submodules can be found here http://progit.org/book/ch6-6.html.

Single line version

git submodule foreach "(git checkout master; git pull; cd ..; git add '$path'; git commit -m 'Submodule Sync')"

Solution 1

To make it simple.

Ran this command to get the latest of the submodule:

git submodule update --remote --mergeor git submodule update --remote

now, we need to update the reference in our parent repo, that can be done using this command:

git add <<submodulesfoldername>>

note: <> == name of my sub module folder git add sub-modules (remember to commit and push with your code, the reference of the latest commit would have been added)

enter image description here

Solution 2

If the above solution doesn't pull the latest, Try This:

git submodule update --init --recursive

None of the above answers worked for me.

This was the solution, from the parent directory run:

git submodule update --init;cd submodule-directory;git pull;cd ..;git add submodule-directory;

now you can git commit and git push

A few of the other answers recommend merging/committing within the submodule's directory, which IMO can become a little messy.

Assuming the remote server is named origin and we want the master branch of the submodule(s), I tend to use:

git submodule foreach "git fetch && git reset --hard origin/master"

Note: This will perform a hard reset on each submodule -- if you don't want this, you can change --hard to --soft.

My project should use the 'latest' for the submodule. On Mac OSX 10.11, git version 2.7.1, I did not need to go 'into' my submodule folder in order to collect its commits. I merely did a regular

git pull --rebase 

at the top level, and it correctly updated my submodule.

Andy's response worked for me by escaping $path:

git submodule foreach "(git checkout master; git pull; cd ..; git add \$path; git commit -m 'Submodule Sync')"