Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Git submodule to latest commit on origin

I have a project with a Git submodule. It is from an ssh://... URL, and is on commit A. Commit B has been pushed to that URL, and I want the submodule to retrieve the commit, and change to it.

Now, my understanding is that git submodule update should do this, but it doesn't. It doesn't do anything (no output, success exit code). Here's an example:

$ mkdir foo $ cd foo $ git init . Initialized empty Git repository in /.../foo/.git/ $ git submodule add ssh://user@host/git/mod mod Cloning into mod... user@host's password: hunter2 remote: Counting objects: 131, done. remote: Compressing objects: 100% (115/115), done. remote: Total 131 (delta 54), reused 0 (delta 0) Receiving objects: 100% (131/131), 16.16 KiB, done. Resolving deltas: 100% (54/54), done. $ git commit -m "Hello world." [master (root-commit) 565b235] Hello world.  2 files changed, 4 insertions(+), 0 deletions(-)  create mode 100644 .gitmodules  create mode 160000 mod # At this point, ssh://user@host/git/mod changes; submodule needs to change too. $ git submodule init Submodule 'mod' (ssh://user@host/git/mod) registered for path 'mod' $ git submodule update $ git submodule sync Synchronizing submodule url for 'mod' $ git submodule update $ man git-submodule  $ git submodule update --rebase $ git submodule update $ echo $? 0 $ git status # On branch master nothing to commit (working directory clean) $ git submodule update mod $ ... 

I've also tried git fetch mod, which appears to do a fetch (but can't possibly, because it's not prompting for a password!), but git log and git show deny the existence of new commits. Thus far I've just been rm-ing the module and re-adding it, but this is both wrong in principle and tedious in practice.

like image 366
Thanatos Avatar asked Apr 29 '11 05:04

Thanatos


People also ask

How do I update a specific submodule?

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.

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 does git submodule update -- Remote do?

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.


1 Answers

The git submodule update command actually tells Git that you want your submodules to each check out the commit already specified in the index of the superproject. If you want to update your submodules to the latest commit available from their remote, you will need to do this directly in the submodules.

So in summary:

# Get the submodule initially git submodule add ssh://bla submodule_dir git submodule init  # Time passes, submodule upstream is updated # and you now want to update  # Change to the submodule directory cd submodule_dir  # Checkout desired branch git checkout master  # Update git pull  # Get back to your project root cd ..  # Now the submodules are in the state you want, so git commit -am "Pulled down update to submodule_dir" 

Or, if you're a busy person:

git submodule foreach git pull origin master 
like image 139
Jason Avatar answered Nov 29 '22 07:11

Jason