Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update git submodule

I have just done a git merge <merging_branch_name> in the app and resolved many conflicts.

But when I do git status, I can see list of submodules with (new commits) in the message. It looks like the submodule (branch/tag) versions have not been updated.

For example:

modified:   plugins/myplugin.git (new commits)

How do I update the submodules with the versions (with the <merging_branch_name>)

I get like this in git bash

my_app (current_branch  |MERGING), 

so when I do git status, I get list of submodules as below

 modified:   plugins/plugin1.git (new commits)
 modified:   plugins/plugin2.git (new commits)
 modified:   plugins/plugin3.git (new commits)
 modified:   plugins/plugin4.git (new commits)

How do I solve this?

like image 817
n92 Avatar asked May 06 '15 06:05

n92


People also ask

How do I update a submodule in git recursive?

If you already cloned the project and forgot --recurse-submodules , you can combine the git submodule init and git submodule update steps by running git submodule update --init . To also initialize, fetch and checkout any nested submodules, you can use the foolproof git submodule update --init --recursive .

Can I edit git 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.

What is git submodule update -- Remote?

For example, submodule update --remote --merge will merge upstream submodule changes into the submodules, while submodule update --merge will merge superproject gitlink changes into the submodules.

How do I sync submodule?

You need to run git submodule sync to apply the remote repo's configuration to your local submodule repos. Note also that, if you are making changes to the submodules, you might want the URLs to mismatch even if the upstream never changed them ... but using multiple remote URLs is probably a better idea for that case.


Video Answer


3 Answers

To update a submodule content to the new SHA1, this should be enough:

# the submodule content needs to be updated
git submodule update --init

# the parent repo needs to record the new submodule SHA1
git add plugins/myplugin
like image 110
VonC Avatar answered Oct 07 '22 01:10

VonC


Let us say you have two branches master and hotifx. In master the head of the submodule is at 123456 in hotfix at abcdef. (let us say that abcdef is newer than 123456). If you have checked out master and merge hotfix the submodule head will move to abcdef but the code in the submodule is not checked out to this new head. If you enter now git diff you will see that the submodule points to 123456 but this is not correct, since you want to point to abcdef. If you would now enter

git add pathToSubmodule

the old 123456 is added to the index. This is the wrong commit. What you have to to is to move the head to the correct commit and this is done by simple calling

git submodule update
like image 42
Michael Mairegger Avatar answered Oct 07 '22 03:10

Michael Mairegger


Submodules basically point to a specific commit in the submodule that was used when making a commit in the parent git repository. It seems that after your merging, the submodules are ahead of what is expected after the merge. You have two choices:

  • go back to the commit where the submodule currently points to
  • update your parent repository to use the new submodule states

The first option was pointed out by the others as

git submodule update

The second option, and I think this is what you are asking for, is done with

git add plugins/plugin?.git
git commit -m 'update submodules'

which will update your HEAD state to store the current submodule references.


You can check what is going on by looking at

git ls-tree HEAD | grep 'plugin1.git'

before and after updating the submodule. Compare this reference to the current commit in this plugin:

cd plugins/plugin1.git
git rev-parse HEAD
like image 44
user1978011 Avatar answered Oct 07 '22 01:10

user1978011