Git 子模块、切换分支以及包含外部 JS 依赖项的推荐方法(天哪)

发布于 2024-10-25 04:19:12 字数 1204 浏览 2 评论 0原文

我有一个 Ruby on Rails 项目(使用 git 进行版本控制),其中包含多个公共 GitHub 存储库中存在的许多外部 JavaScript 依赖项。将这些依赖项包含在我的存储库中(当然,除了手动复制它们)以允许我控制它们何时更新的最佳方式是什么?

git 子模块似乎是完美的方法,但在多个分支之间切换时会导致问题,其中许多分支不包含相同的子模块。

如果 git 子模块是最好的方法,我想我真正的问题是:如何在许多分支中使用子模块而不总是遇到这个问题:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的巷 2024-11-01 04:19:12

子模块的替代方案是 子树合并策略。从该页面复制(它有更多信息,仅供参考):

在此示例中,假设您有
存储库位于 /path/to/B (但它
如果需要,也可以是 URL)。
你想合并 master 分支
该存储库到 dir-B
当前分支中的子目录。

这是您需要的命令序列:

$ 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 "合并 B 项目作为我们的子目录"
$ git pull -s 子树 Bproject master

我个人觉得这很繁琐(我更喜欢子模块,即使有您提到的问题),尽管很多人都坚信它。

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.

泡沫很甜 2024-11-01 04:19:12

两者这个博客这篇文章报告了相同的警告:

这是因为目前还不能很好地支持子模块删除。关于如何让 git 更好地处理这个问题已经有很多讨论,但到目前为止还没有人实现它。但它在我的待办事项列表中,所以请继续关注...

(Jens Lehmann 当时说道,2010 年 11 月)。

现在(2011 年 3 月),我没有看到(或者我错过了)这方面的任何改进。

在您的情况下,git checkout -f -qdevelopment会做什么?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文