Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git submodules, switching branches, and the recommended way to include external JS dependencies (oh my)

I have a Ruby on Rails project (versioned with git) that includes a number of external JavaScript dependencies that exist in various public GitHub repositories. What's the best way to include those dependencies in my repository (aside, of course, from just manually copying them in) in a way that allows me to control when they get updated?

git submodules seem like the perfect way to go, but it causes problems when switching among several branches, many of which don't contain the same submodules.

If git submodules are the best way to do it, I suppose my real question is: How can I use submodules among many branches without running into this problem all the time:

my_project[feature/new_feature√]$ git submodule update 
Cloning into public/javascripts/vendor/rad_dependency...
remote: Counting objects: 34, done.
remote: Compressing objects: 100% (29/29), done.
remote: Total 34 (delta 9), reused 0 (delta 0)
Receiving objects: 100% (34/34), 12.21 KiB, done.
Resolving deltas: 100% (9/9), done.
Submodule path 'public/javascripts/vendor/rad_dependency': checked out '563b51c385297c40ff01fd2f095efb14dbe736e0'

my_project[feature/new_feature√]$ git checkout develop 
warning: unable to rmdir public/javascripts/milkshake/lib/cf-exception-notifier-js: Directory not empty
Switched to branch 'develop'

my_project[develop⚡]$ git status
# On branch develop
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   public/javascripts/milkshake/lib/cf-exception-notifier-js/
nothing added to commit but untracked files present (use "git add" to track)
like image 332
Tyson Avatar asked Mar 21 '11 21:03

Tyson


Video Answer


2 Answers

An alternative to submodules is the subtree merge strategy. Copied from that page (it has more info, this is purely for reference):

In this example, let’s say you have the repository at /path/to/B (but it can be an URL as well, if you want). You want to merge the master branch of that repository to the dir-B subdirectory in your current branch.

Here is the command sequence you need:

$ git remote add -f Bproject /path/to/B 
$ git merge -s ours --no-commit Bproject/master <2> 
$ git read-tree --prefix=dir-B/ -u Bproject/master
$ git commit -m "Merge B project as our subdirectory"
$ git pull -s subtree Bproject master

I personally find this fiddly (I prefer submodules, even with the issus you've mentioned), although a lot of people swear by it.

like image 55
Hamish Avatar answered Oct 22 '22 14:10

Hamish


Both this blog and this post reports the same warning:

This is because submodule deletion is not well supported at the moment. There has been a lot of discussion how to make git handle that better but no one implemented it so far. But it's on my ToDo list, so stay tuned ...

(said Jens Lehmann at the time, November 2010).

Right now (March 2011), I don't see (or I missed) any improvment on that front.

What would a git checkout -f -q develop do in your case?

like image 33
VonC Avatar answered Oct 22 '22 14:10

VonC