What is a git submodule for?

Asked

Viewed 1,303 times

13

One day I went to clone a library in the github and there was an instruction to use the command git submodule update.

I’ve been working with the git and until then had no knowledge of this command.

What is this command for submodule?

3 answers

6

This command is useful when you want to insert another git repository into your main project (a library, for example).

For git, the submodule will be a completely independent project. You can edit the contents below the submodule folder, commit, push, etc. As long as you have permission to do so :)

There are alternatives, such as package managers, that organize dependencies in other, more efficient and secure ways.

  • In this last paragraph, then we would have as an example the composer, npm and the gem.

  • Exactly! These managers usually analyze project dependencies, download the files into a specific folder, and it’s out of control for git versions in your project (.gitignore). In general you can update everything with a simple command line. I like to use submodules when I’m also making modifications to the submodule itself as well. Otherwise, I don’t recommend.

3

Submodule is for you to manage your repository modules!

What does that mean?!

Let’s say you’re creating a program and need to add a library you found on github to facilitate development. You can add this library as a sub module of your application.

What’s the benefit of using this instead of just copying the code?

If you just copy the code:

  • You may have problems with the license of the code you are using
  • Lose the library history you are using
  • If there is any change in the library code you will not know this unless you are constantly copying the code

If you use the submodule:

  • If there is an update in the library you can easily update it
  • You probably won’t have a problem with code licensing
  • Decreases the size of your repository

Like the submodule works in practice?

Simply put, it can be said that it maintains a pointer to the repository

For more details see https://git-scm.com/docs/git-submodule

  • Just left to say that a package manager (npm, Composer, etc) is even simpler/advantageous than a submodule

  • Yes! But not all languages have package managers, for example C language (at least I don’t know one)

0

In fact, the command git submodule update tells Git that you want your submodules to check out the commit already specified in the superproject index. If you want upgrade your submodules for the latest confirmation available from the remote control, you will need to do this directly in the submodules. 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"

In case you are lazy ( kkk )

git submodule foreach git pull origin master

Browser other questions tagged

You are not signed in. Login or sign up in order to post.