Single file as Git submodule

If you can use symlinks (e.g. you are not using Windows), then you can set up core and core.php like this:

# "base" repository layout:
core/
core.app

# each app repository layout:
base/
  core/
  core.php
core -> base/core/
core.php -> base/core.php
app/

In each app repository, the base/ directory is either a submodule that uses the “base” repository or a subtree merge of the “base” repository.

Both methods will let you start making changes to the base code in the context of a particular app and later pull those changes back into the main base repository. When using submodules you have to be careful to always publish new base commits before publishing any app commits that reference those new base commits (this is not a problem when using subtree merges because each app is “flat” and effectively has its own copy of the base).

The third-party git subtree command seems like a very nice way to manage the subtree merge, if you decide against submodules.

Subtree

git init newapp
cd newapp
ln -s base/core
ln -s base/core.php
git add core core.php
git commit -m'point to base (to be added next)'

# hook up base
git subtree add --prefix=base [email protected]:me/app_base.git master

mkdir app
# edit app/bar.php

# update base
git subtree pull --prefix=base [email protected]:me/app_base.git master

.
|-- .git/
|   |-- ...
|   `-- ...
|-- app/
|   `-- bar.php
|-- base/
|   |-- core/
|   |   `-- foo.php
|   `-- core.php
|-- core -> base/core/
`-- core.php -> base/core.php

Submodule

git init newapp
cd newapp
ln -s base/core
ln -s base/core.php
git add core core.php
git commit -m'point to base (to be added next)'

# hook up "base"
git submodule add [email protected]:me/app_base.git base
git commit -m'incorporate base'

mkdir app
# edit app/bar.php

# update base
(cd base && git fetch origin && git merge origin/master)
git add base
git commit -m'updated base'
.
|-- .git/
|   |-- ...
|   `-- ...
|-- .gitmodules
|-- app/
|   `-- bar.php
|-- base/
|   |-- .git/
|   |   |-- ...
|   |   `-- ...
|   |-- core/
|   |   `-- foo.php
|   `-- core.php
|-- core -> base/core/
`-- core.php -> base/core.php

Leave a Comment

tech