Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git submodule init not pulling latest commit

I have a git repo with a git submodule inside of it. The submodule is hosted on bitbucket. I want to update my local copy of the submodule to its latest commit. I tired "git submodule update" however that is not doing anything. So I tried deleting the submodule folder and then doing "git submodule init" However it simply pulls the initial submodule commit, not the latest.

How can I get my local submodule to update to the latest commit?

like image 821
levi Avatar asked Dec 12 '12 17:12

levi


People also ask

What does git submodule init do?

Submodules allow you to keep a Git repository as a subdirectory of another Git repository. This lets you clone another repository into your project and keep your commits separate.

Does git pull also update submodules?

With Git 2.34, if the repository is cloned with the --recurse-submodules , a simple git pull will recurse into submodules.

Why is submodule dirty?

Submodules are now regarded as dirty if they have any modified files or untracked files, whereas previously it would only be the case if HEAD in the submodule pointed to the wrong commit.


3 Answers

Git is doing exactly what it's supposed to be doing. git submodule update will set your submodule to what the current commit in the parent repo specifies the submodule should be at. This way you can checkout another branch, older commit or tag, then run git submodule update and the submodule will be set to what that reference expects so your entire solution will have it's dependencies satisfied.

What you need to do is:

cd mysubmoduledir
git fetch
git checkout master # or any other branch that you need the latest of
git merge origin/master
cd -  # go back to the top repo
git status # should show that your submodule changed
git add mysubmoduledir
git commit -m "Updated my solution to use latest sub project."

a shorter version is:

cd mysubmoduledir
git pull # assumes you are already on the branch you need to be on
cd -
git commit -am "Updated submodule" # assumes you had no other modified files

The word "update" is not the best for this submodule command. It really means "point the submodule to the commit that the parent repo's commit expects".

Updating a submodule to a different commit (doesn't have to be the latest) requires you to cd into that directory, manipulate it like a regular git repo so the current commit is what you want, then go back out and commit this change on the top level repo.

like image 71
Adam Dymitruk Avatar answered Oct 18 '22 00:10

Adam Dymitruk


Simply try below command after you have added your submodules.

for git v 1.8.x

git submodule update --init --recursive --remote

for v1.7.x:

git submodule update --init --recursive or git pull --recurse-submodules

for v1.6.x

git submodule foreach git pull origin master

to check your git version.

git version
like image 29
Saad Avatar answered Oct 18 '22 01:10

Saad


Each git commit which includes a submodule ties to a particular commit within the submodule repository.

The git submodule update command is provided to update the checked out version of the submodule to the commit which is recorded for the current version of the parent repository. This may actually be an older version than what you currently have checked out in that submodule, for instance if you are examining an old version of the parent repository which used an older version of the submodule.

To get a newer version of the submodule, switch into that directory and update it as you would any other git repository (e.g. pull from the upstream repository). If you make any new commits or you pulled from a repository different from the one you use as the source for the submodule, make sure to push your changes out. After this is done you can return to the parent repository and commit the change to the submodule to record the new submodule commit that you are now using for the submodule in the current version as well as future versions until you make another update.

like image 11
qqx Avatar answered Oct 17 '22 23:10

qqx