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
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 commit the change so the gitlink in the parent repository is updated
git commit
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.
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/projA
git 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.
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.
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.
Single line version
git submodule foreach "(git checkout master; git pull; cd ..; git add '$path'; git commit -m 'Submodule Sync')"
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
.