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?
In order to update an existing Git submodule, you need to execute the “git submodule update” with the “–remote” and the “–merge” option. Using the “–remote” command, you will be able to update your existing Git submodules without having to run “git pull” commands in each submodule of your project.
git submodule update --remote will only update the branch registered in the . gitmodule , and by default, you will end up with a detached HEAD, unless --rebase or --merge is specified or the key submodule.
If you want to make a change within a submodule, you should first check out a branch, make your changes, publish the change within the submodule, and then update the superproject to reference the new commit.
git submodule update --init --recursive --remote - updates all submodules recursively along their tracking branches. Without the --remote , it'll reset the submodule working directories to the "right" commit for the parent.
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.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With