Skip to content
Magnus Karlsson edited this page Feb 9, 2016 · 14 revisions

Do you need to share code between git repositories? git-subrepo can help you with this!

Start with installing git subrepo and make sure that you can run

$ git subrepo
usage: git subrepo <command> <arguments> <options>
...

Let us start with creating a new repository in the shared directory where we have our common files bar and foo.

$ cd /tmp/shared
$ git init
$ git add .
$ git commit -m "Initial revision of our shared code"
[master (root-commit) 58e908c] Initial revision of our shared code
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 bar
 create mode 100644 foo

Then we clone our project repository.

$ git clone /tmp/project_bare/ project
Cloning into 'project'...
done.

To use the shared repository as a subrepo inside the project.

$ cd /tmp/project
$ git subrepo clone /tmp/shared/ shared
Subrepo '../shared' (master) cloned into 'shared'.
$ git ls-files
alpha
beta
shared/.gitrepo
shared/bar
shared/foo
$ git log --pretty=oneline
0a6dceb7f0d97b690db5338d5a4d6016ca9548f6 git subrepo clone ../shared shared
b228b44453645162664ccaea8a4ab210ec33df66 Initial project commit

As you can see shared has been a part of the project repository, the only thing that tells us that there is a subrepo is the .gitrepo file. This file is used to store some subrepo essential information.

$ cat shared/.gitrepo 
[subrepo]
        remote = ../shared
        branch = master
        commit = 58e908c601bfb346fad0bd639d78415474db0ffd
        parent = b228b44453645162664ccaea8a4ab210ec33df66
        cmdver = 0.3.0

Now you can start to work on your project.

$ touch delta
$ echo "123" >> shared/foo
$ git add .
$ git commit -m "Add delta, edit shared/foo"
[master c9b9003] Add delta, edit shared/foo
 2 files changed, 1 insertion(+)
 create mode 100644 delta
$ echo "abc" >> alpha
$ echo "def" >> beta
$ git add .
$ git commit -m "Edit alpha, beta"
[master e589068] Edit alpha, beta
 2 files changed, 2 insertions(+)
$ touch shared/fie
$ git add .
$ git commit -m "Add shared/fie"
[master 0468d40] Add shared/fie
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 shared/fie
$ git log --pretty=oneline
0468d4081300b8c2bce657c8145c7a353decbc85 Add shared/fie
e58906879f85bee65b7e448a4034b60ef789e428 Edit alpha, beta
c9b9003621c75dca6636a1ed5c8ad5663b255016 Add delta, edit shared/foo
0a6dceb7f0d97b690db5338d5a4d6016ca9548f6 git subrepo clone ../shared shared
b228b44453645162664ccaea8a4ab210ec33df66 Initial project commit

Some changes and new files. So far we have only worked in our local repository. To share our changes to others we perform a regular push.

$ git push
Counting objects: 11, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (11/11), 890 bytes | 0 bytes/s, done.
Total 11 (delta 4), reused 0 (delta 0)
To /tmp/project_bare/
   0a6dceb..0468d40  master -> master

Now the changes are available to everyone working on the project repository. But if we look at our shared subrepo we will see that nothing has happened there.

$ cd /tmp/shared
$ git log --pretty=oneline
58e908c601bfb346fad0bd639d78415474db0ffd Initial revision of our shared code

Why? Because the subrepo will not update unless we specifically tell it to. Lets say that we add another file in the subrepo.

$ cd /tmp/shared
$ touch 
Clone this wiki locally