2

I have a framework I am building to make some common functions (database connection) easier and would like to use it in another project. I could always just copy and paste it, but I am working on it and expanding it constantly. It would be much easier to just have it copy its updated self over whenever I do a commit in my Git repo.

Could I use Git to automatically update the framework in my other project or will I have to find another solution?

Andrew M
  • 4,208
  • 11
  • 42
  • 67

2 Answers2

5

Use either git subtrees or git submodules.

Last time I've checked, submodules had some major issues (but I've heard that they were improved a bit in recent versions). See here for details: Git submodules workflow

In short:

Subtrees are regular Git branches, merged with special merge strategy, which puts all their files into a subdirectory of the host repository working copy. Aside of the merge strategy (which matters only when merge commit is created), Git "subtree" is a perfectly normal Git branch.

Pros:

  • No extra tool support needed, except when you need to pull in new changes from the subtree, and even then all support is the -s subtree command line option (see here for the full workflow). You need to remember about subtree only when you do the subtree pull.

Cons:

  • All subtree commits are visible in the history of the host project.
  • Don't commit changes to the host repository's directory where subtree lives in or face conflicts on next subtree pull.

Submodules are Git repositories inside the host repository.

Pros:

  • Cleaner history, better separation from the host repository.
  • One may commit directly to the submodule upstream from the submodule repository directly (or so I think).

Cons:

  • For each (or many) Git command you have remember that you're dealing with repository with submodules, lots of nasty corner cases (or so it was when I looked). Special tool support required to work with each clone of the repository.

Disclaimer: I'm biased against submodules. Try and see for yourself, what approach is better for your workflow.

Community
  • 1
  • 1
Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160
  • Submodules looks like what I need. Anyone know of a visual Git client for the Mac (think Tower, Cornerstone [though that is for SVN]) that works with submodules? – Andrew M Feb 21 '11 at 01:43
  • 1
    Experiment and make sure that submodules really fit to your workflow before you commit to them. I wasted a lot of time (see the question I linked) only to realize that I can't use them at all. – Alexander Gladysh Feb 21 '11 at 01:48
  • git submodules are nice. here is a nice example of using submodules. http://chrisjean.com/2009/04/20/git-submodules-adding-using-removing-and-updating/ – Cesar A. Rivas Feb 21 '11 at 01:54
  • I am looking around at subtrees but I am not sure what the difference is... I will likely be the only one maintaining these two projects (framework and the project), but I am just trying to separate out the two so that I have the option to use the framework at a later date on another project without having to copy-paste. When would subtrees work better, and what (if any) Git clients (for Mac) have support for subtrees or submodules? I could always use the command line, but I would prefer to do everything in a GUI... – Andrew M Feb 21 '11 at 02:02
  • @Caesar: Well, I linked to a post with a list of reasons why submodules are (or were) not acceptable to my workflow. If you find them nice for yours — no problem with that. I'm happy with subtrees. – Alexander Gladysh Feb 21 '11 at 02:05
  • @Andrew: I've updated my answer. (Also look here: http://www.kernel.org/pub/software/scm/git/docs/howto/using-merge-subtree.html) – Alexander Gladysh Feb 21 '11 at 02:11
  • @Andrew: Sorry, can't advise on tool support — I'm not a fan of working with Git from GUI idea. Anyway, I urge you to actually try both ways yourself with whatever tools are at your disposal, and pick the one which suits your workflow best **in practice**. – Alexander Gladysh Feb 21 '11 at 02:26
1

git-submodules.

You will have a few issues with updating in different places, but it should work out.

sykora
  • 96,888
  • 11
  • 64
  • 71