git submodule init not pulling latest commit
Asked Answered
O

4

48

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?

Ossian answered 12/12, 2012 at 17:10 Comment(0)
M
55

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.

Mercurous answered 12/12, 2012 at 17:49 Comment(6)
What a ridiculous name for a command?! It obviously should be git submodule etadpuEarleanearleen
Thinking about this "git submodule ilikepancakes" would probably make just as much sense. And if anybody disagrees, please read a wise presentation about the design of kernel space apis: man7.org/conf/lca2013/…Earleanearleen
More details on using git submodule: chrisjean.com/git-submodules-adding-using-removing-and-updatingHumeral
So there is no way to pull all submodules? you need to go on each submodule folder and do git pull?Underneath
Sweet baby Jesus, this was killing me. TY!Shote
@slayerbleast Yes there is: git submodule foreach git pull origin masterAlsace
D
40

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
Doublereed answered 21/4, 2017 at 7:11 Comment(0)
M
10

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.

Misconceive answered 12/12, 2012 at 17:49 Comment(0)
S
1

The somewhat counterintuitive issue here (or at least it was for me is this): If the newest version of your main repository gets out-of-sync with your submodule, then when you try and "update" the submodule by running git submodule update from within the main repository, it will only pull up to the latest changes SPECIFIED IN THE PARENT, and not in the submodule.

In other words, your parent project could be pointing to the wrong commit of your submodule. To ensure that your parent is pointing to the latest submodule commit (which is my case what I wanted), navigate into the submodule and run git pull and then return to the parent and run git add .

Next, if you run the git diff command you'll notice that although none of the files have changed in the main repository, you'll get a dirty message showing you that the submodule now points to a new commit. That's what you want. Just commit the changes and push them up, and your two projects should be re-synchronized.

Make sure that in the future, if you make changes to the submodule, that you also add them to the parent repository using git add . (you'll notice a dirty message when the submodule changes).

Strath answered 18/7, 2020 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.