How to Use git submodule to Include Another Repository
When you want to use common CSS or a library across several projects without copying it, use git submodule. The included repository keeps its own history and is placed as a single folder inside your repository. This article covers four operations, adding, fetching after a clone, pulling in updates from the shared repository, and removing, with the output before and after each one.
I tested on Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>) and fixed the commit dates with environment variables. The working folders were created under /path/to, so that is the path shown in the output. The shared repository is not on a server such as GitHub. It is a bare repository created on the same machine (/path/to/remote/neko-ui.git). I did not test adding with HTTPS or SSH URLs, nested submodules (a submodule that contains another submodule), working and committing inside a submodule, or Windows and macOS.
Repositories used in the test
The shared side is the neko-ui repository, which contains only the button CSS (button.css). The consuming side is the blog repository, which contains only the top page (index.html). index.html loads vendor/neko-ui/button.css, and neko-ui is added as a submodule at that location.
This is the state of blog before the operations. git submodule status prints nothing.
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
$ ls -a
.
..
.git
index.html
$ git submodule status
Adding with git submodule add
Add a submodule with git submodule add <repository URL> <path>. On the first try in the test environment, the command was stopped as follows.
$ git submodule add /path/to/remote/neko-ui.git vendor/neko-ui
Cloning into '/path/to/blog/vendor/neko-ui'...
fatal: transport 'file' not allowed
fatal: clone of '/path/to/remote/neko-ui.git' into submodule path '/path/to/blog/vendor/neko-ui' failed
According to the description of protocol.allow and protocol.<name>.allow in the official git config reference, the https and ssh protocols are allowed by default, but local paths (file) are not allowed when Git clones automatically, as it does when adding a submodule. You do not need the following setting when adding with an https or ssh URL. Here, I set protocol.file.allow in the user configuration (--global) of this disposable test environment and then ran the add again.
$ git config --global protocol.file.allow always
$ git submodule add /path/to/remote/neko-ui.git vendor/neko-ui
Cloning into '/path/to/blog/vendor/neko-ui'...
done.
This is the state after adding. Two items were staged: the configuration file .gitmodules and vendor/neko-ui.
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: .gitmodules
new file: vendor/neko-ui
$ cat .gitmodules
[submodule "vendor/neko-ui"]
path = vendor/neko-ui
url = /path/to/remote/neko-ui.git
$ ls -a vendor/neko-ui
.
..
.git
button.css
.gitmodules records the location (path) and the source to fetch from (url). According to the official reference, this URL is used later when someone who clones blog fetches the submodule.
The files inside vendor/neko-ui are not included in the commit of blog. blog records only the commit ID that indicates which commit of neko-ui to use.
$ cat vendor/neko-ui/.git
gitdir: ../../.git/modules/vendor/neko-ui
$ git ls-files -s vendor/neko-ui
160000 e2869d7cb647bc3b3aa8374e5d0f02881e80b9d9 0 vendor/neko-ui
$ git diff --cached vendor/neko-ui
diff --git a/vendor/neko-ui b/vendor/neko-ui
new file mode 160000
index 0000000..e2869d7
--- /dev/null
+++ b/vendor/neko-ui
@@ -0,0 +1 @@
+Subproject commit e2869d7cb647bc3b3aa8374e5d0f02881e80b9d9
The official gitsubmodules documentation calls this record, with mode 160000, a gitlink. The history of neko-ui itself is stored under .git/modules/ in blog, and vendor/neko-ui/.git is a file that points to that location.

I committed these two changes and pushed them to the remote of blog (remote/blog.git).
$ git commit -q -m 'neko-ui を submodule として追加'
$ git push -q origin main
$ git log --oneline
dd709f3 neko-ui を submodule として追加
fa09f72 トップページを追加
Fetching submodule contents after cloning
I cloned blog normally into another folder (blog2). The vendor/neko-ui folder was created, but it was empty. The - at the beginning of the git submodule status output means the submodule has not been initialized yet.
$ git clone -q remote/blog.git blog2
$ ls -a blog2/vendor/neko-ui
.
..
$ git -C blog2 submodule status
-e2869d7cb647bc3b3aa8374e5d0f02881e80b9d9 vendor/neko-ui
git submodule update --init clones from the URL in .gitmodules and checks out the recorded commit.
$ git -C blog2 submodule update --init
Submodule 'vendor/neko-ui' (/path/to/remote/neko-ui.git) registered for path 'vendor/neko-ui'
Cloning into '/path/to/blog2/vendor/neko-ui'...
done.
Submodule path 'vendor/neko-ui': checked out 'e2869d7cb647bc3b3aa8374e5d0f02881e80b9d9'
$ git -C blog2 submodule status
e2869d7cb647bc3b3aa8374e5d0f02881e80b9d9 vendor/neko-ui (heads/main)
$ ls blog2/vendor/neko-ui
button.css
Adding --recurse-submodules to the clone completed these two steps in one command.
$ git clone -q --recurse-submodules remote/blog.git blog3
$ git -C blog3 submodule status
e2869d7cb647bc3b3aa8374e5d0f02881e80b9d9 vendor/neko-ui (heads/main)
Inside the submodule that git submodule update checked out, no branch was checked out (detached HEAD). This is because, as the official reference explains, the recorded commit itself is checked out.
$ git -C blog2/vendor/neko-ui status
HEAD detached at e2869d7
nothing to commit, working tree clean
Pulling in new commits from the shared repository
I added a commit that rounds the button corners (6f735c5) to neko-ui and pushed it. At this point, the submodule in blog is still at the previous commit, e2869d7. A submodule is pinned to the commit it records, so it does not change on its own when the shared repository moves forward.
Running git submodule update --remote fetched from the shared repository and switched the submodule to the latest commit.
$ git submodule status
e2869d7cb647bc3b3aa8374e5d0f02881e80b9d9 vendor/neko-ui (heads/main)
$ git submodule update --remote
From /path/to/remote/neko-ui
e2869d7..6f735c5 main -> origin/main
Submodule path 'vendor/neko-ui': checked out '6f735c5c9767493b5ceef96f2f2f1869b9235b46'
$ git submodule status
+6f735c5c9767493b5ceef96f2f2f1869b9235b46 vendor/neko-ui (remotes/origin/HEAD)
The + at the beginning means that the checked-out commit differs from the commit recorded in blog. git status showed vendor/neko-ui as changed. git diff --submodule=log shows which commits are coming in.
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: vendor/neko-ui (new commits)
no changes added to commit (use "git add" and/or "git commit -a")
$ git diff --submodule=log
Submodule vendor/neko-ui e2869d7..6f735c5:
> ボタンの角を丸くする
Once you decide to use the new commit, run git add and commit, the same as for a regular file. This changes the commit ID that blog records to 6f735c5.
$ git add vendor/neko-ui
$ git commit -q -m 'neko-ui を更新'
$ git push -q origin main
$ git submodule status
6f735c5c9767493b5ceef96f2f2f1869b9235b46 vendor/neko-ui (remotes/origin/HEAD)
Other clones need update after pull
When I ran git pull in blog2, which I had cloned earlier, the recorded commit ID changed, but the files in the submodule stayed old. A + appeared at the beginning of the git submodule status output, and button.css did not have the rounding line.
$ git pull -q
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: vendor/neko-ui (new commits)
no changes added to commit (use "git add" and/or "git commit -a")
$ git submodule status
+e2869d7cb647bc3b3aa8374e5d0f02881e80b9d9 vendor/neko-ui (heads/main)
$ git diff
diff --git a/vendor/neko-ui b/vendor/neko-ui
index 6f735c5..e2869d7 160000
--- a/vendor/neko-ui
+++ b/vendor/neko-ui
@@ -1 +1 @@
-Subproject commit 6f735c5c9767493b5ceef96f2f2f1869b9235b46
+Subproject commit e2869d7cb647bc3b3aa8374e5d0f02881e80b9d9
$ cat vendor/neko-ui/button.css
.button {
color: #fff;
}
The git diff output is a diff that goes from the recorded 6f735c5 back to the older e2869d7. To check what would happen, I ran git add in this state, and the change that reverts to the older commit was staged. After confirming this, I undid it with git restore --staged.
$ git add vendor/neko-ui
$ git diff --cached
diff --git a/vendor/neko-ui b/vendor/neko-ui
index 6f735c5..e2869d7 160000
--- a/vendor/neko-ui
+++ b/vendor/neko-ui
@@ -1 +1 @@
-Subproject commit 6f735c5c9767493b5ceef96f2f2f1869b9235b46
+Subproject commit e2869d7cb647bc3b3aa8374e5d0f02881e80b9d9
$ git restore --staged vendor/neko-ui
$ git status --short
M vendor/neko-ui
Here I did not run git add. Instead, I ran git submodule update without --remote to match the commit recorded in blog.
$ git submodule update
Submodule path 'vendor/neko-ui': checked out '6f735c5c9767493b5ceef96f2f2f1869b9235b46'
$ git submodule status
6f735c5c9767493b5ceef96f2f2f1869b9235b46 vendor/neko-ui (remotes/origin/HEAD)
$ cat vendor/neko-ui/button.css
.button {
color: #fff;
border-radius: 8px;
}
The difference is this: with --remote, the submodule moves to the latest commit in the shared repository; without it, the submodule moves to the commit recorded in blog.
In blog3, I used git pull --recurse-submodules. The submodule was updated to the recorded commit at the same time as the pull, and git status --short printed nothing. According to the description of submodule.recurse in the official git config reference, setting this to true has the same effect as adding --recurse-submodules to every pull and similar command.
$ git pull -q --recurse-submodules
$ git submodule status
6f735c5c9767493b5ceef96f2f2f1869b9235b46 vendor/neko-ui (remotes/origin/HEAD)
$ git status --short
Removing a submodule
To remove a submodule you no longer need, use git rm.
$ git rm vendor/neko-ui
rm 'vendor/neko-ui'
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: .gitmodules
deleted: vendor/neko-ui
$ ls -a vendor
.
..
$ ls .git/modules/vendor
neko-ui
The vendor/neko-ui folder was deleted, and the section for this submodule was removed from .gitmodules. (In the test, there were no other submodules, so .gitmodules became 0 bytes.) Both changes are staged, so committing them completes the removal.
However, .git/modules/vendor/neko-ui remained. According to the official gitsubmodules documentation, Git keeps this folder so that the submodule can be restored later, when an older commit is checked out, without fetching it again. If you want to delete it completely, remove this folder yourself.



