Updating modules

It often happens that while working on one project, you need to use another project from within it.Perhaps it’s a library that a third party developed or that you’re developing separately and using in multiple parent projects.A common issue arises in these scenarios: you want to be able to treat the two projects as separate yet still be able to use one from within the other. Suppose you’re developing a website and creating Atom feeds.

updating modules-48updating modules-57updating modules-33

You’re likely to have to either include this code from a shared library like a CPAN install or Ruby gem, or copy the source code into your own project tree. Submodule path 'Db Connector': checked out 'c3f01dc8862123d317dd46284b05b6892c7b29bc' The simplest model of using submodules in a project would be if you were simply consuming a subproject and wanted to get updates from it from time to time but were not actually modifying anything in your checkout. $ git submodule update --remote Db Connector remote: Counting objects: 4, done. remote: Total 4 (delta 2), reused 4 (delta 2) Unpacking objects: 100% (4/4), done.

The issue with including the library is that it’s difficult to customize the library in any way and often more difficult to deploy it, because you need to make sure every client has that library available. remote: Total 11 (delta 0), reused 11 (delta 0) Unpacking objects: 100% (11/11), done. From https://github.com/chaconinc/Db Connector 3f19983..d0354fc master - branch of the submodule repository.

The issue with copying the code into your own project is that any custom changes you make are difficult to merge when upstream changes become available. Submodules allow you to keep a Git repository as a subdirectory of another Git repository. By default, submodules will add the subproject into a directory named the same as the repository, in this case “Db Connector”. Submodule 'Db Connector' (https://github.com/chaconinc/Db Connector) registered for path 'Db Connector' Cloning into 'Db Connector'... You can, however, set this to something different if you want.

This lets you clone another repository into your project and keep your commits separate. remote: Total 11 (delta 0), reused 11 (delta 0) Unpacking objects: 100% (11/11), done. You can add a different path at the end of the command if you want it to go elsewhere. remote: Total 14 (delta 1), reused 13 (delta 0) Unpacking objects: 100% (14/14), done. For example, if you want to have the Db Connector submodule track that repository’s “stable” branch, you can set it in either your $ git config -f .gitmodules submodule.

$ git submodule add https://github.com/chaconinc/Db Connector Cloning into 'Db Connector'... Since the URL in the .gitmodules file is what other people will first try to clone/fetch from, make sure to use a URL that they can access if possible. Submodule path 'Db Connector': checked out 'c3f01dc8862123d317dd46284b05b6892c7b29bc'$ git clone --recurse-submodules https://github.com/chaconinc/Main Project Cloning into 'Main Project'... Db Connector.branch stable $ git submodule update --remote remote: Counting objects: 4, done. remote: Total 4 (delta 2), reused 4 (delta 2) Unpacking objects: 100% (4/4), done.

For example, if you use a different URL to push to than others would to pull from, use the one that others have access to. remote: Total 11 (delta 0), reused 11 (delta 0) Unpacking objects: 100% (11/11), done. From https://github.com/chaconinc/Db Connector 27cf5d3..c87d55d stable -$ git diff diff --git a/.gitmodules b/.gitmodules index 6fc0b3d..fd1cc29 100644 --- a/.gitmodules b/.gitmodules @@ -1,3 1,4 @@ [submodule "Db Connector"] path = Db Connector url = https://github.com/chaconinc/Db Connector branch = stable Submodule Db Connector c3f01dc..c87d55d: Date: Wed Sep 17 2014 0200 updating Db Connector for bug fixes diff --git a/.gitmodules b/.gitmodules index 6fc0b3d..fd1cc29 100644 --- a/.gitmodules b/.gitmodules @@ -1,3 1,4 @@ [submodule "Db Connector"] path = Db Connector url = https://github.com/chaconinc/Db Connector branch = stable Submodule Db Connector c3f01dc..c87d55d: It’s quite likely that if you’re using submodules, you’re doing so because you really want to work on the code in the submodule at the same time as you’re working on the code in the main project (or across several submodules).

You can overwrite this value locally with $ git diff --cached Db Connector diff --git a/Db Connector b/Db Connector new file mode 160000 index 0000000..c3f01dc --- /dev/null b/Db Connector @@ -0,0 1 @@ Subproject commit c3f01dc8862123d317dd46284b05b6892c7b29bc is a subdirectory in your working directory, Git sees it as a submodule and doesn’t track its contents when you’re not in that directory. $ cd Main Project $ ls -la total 16 drwxr-xr-x 9 schacon staff 306 Sep 17 . drwxr-xr-x 13 schacon staff 442 Sep 17 -rw-r--r-- 1 schacon staff 92 Sep 17 .gitmodules drwxr-xr-x 2 schacon staff 68 Sep 17 Db Connector -rw-r--r-- 1 schacon staff 756 Sep 17 Makefile drwxr-xr-x 3 schacon staff 102 Sep 17 includes drwxr-xr-x 4 schacon staff 136 Sep 17 scripts drwxr-xr-x 4 schacon staff 136 Sep 17 src $ cd Db Connector/ $ ls $$ git submodule init Submodule 'Db Connector' (https://github.com/chaconinc/Db Connector) registered for path 'Db Connector' $ git submodule update Cloning into 'Db Connector'... Otherwise you would probably instead be using a simpler dependency management system (such as Maven or Rubygems).

Instead, Git sees it as a particular commit from that repository. remote: Total 14 (delta 1), reused 13 (delta 0) Unpacking objects: 100% (14/14), done. command to fetch changes from the submodule repositories, Git would get the changes and update the files in the subdirectory but will leave the sub-repository in what’s called a “detached HEAD” state.

$ git diff --cached --submodule diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..71fc376 --- /dev/null b/.gitmodules @@ -0,0 1,3 @@ [submodule "Db Connector"] path = Db Connector url = https://github.com/chaconinc/Db Connector Submodule Db Connector 0000000...c3f01dc (new submodule)$ git clone https://github.com/chaconinc/Main Project Cloning into 'Main Project'... This means that there is no local working branch (like “master”, for example) tracking changes.

Tags: , ,