Gemfile.lock 应该包含在 .gitignore 中吗?
我对捆绑器及其生成的文件有点陌生。我有一份来自 GitHub 的 git 存储库副本,该存储库由许多人贡献,因此我惊讶地发现捆绑程序创建了一个存储库中不存在且不在 .gitignore 列表。
因为我已经分叉了它,所以我知道将它添加到存储库不会破坏主存储库的任何内容,但如果我执行拉取请求,会导致问题吗?
Gemfile.lock
是否应该包含在存储库中?
I'm sort of new to bundler and the files it generates. I have a copy of a git repo from GitHub that is being contributed to by many people so I was surprised to find that bundler created a file that didn't exist in the repo and wasn't in the .gitignore
list.
Since I have forked it, I know adding it to the repo won't break anything for the main repo, but if I do a pull request, will it cause a problem?
Should Gemfile.lock
be included in the repository?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
当您正在开发需要具有可配置数据库适配器的开源 Rails 应用程序时,真正的问题就会发生。我正在开发 Fat Free CRM 的 Rails 3 分支。
我的偏好是 postgres,但我们希望默认数据库是 mysql2。
在这种情况下,仍然需要使用默认的 gem 集签入
Gemfile.lock
,但我需要忽略我在计算机上对其所做的更改。为了实现这一点,我运行:并反转:
在
Gemfile
中包含类似以下代码的内容也很有用。这将根据您的database.yml 加载适当的数据库适配器gem。我不能说这是否是既定的最佳实践,但它对我来说效果很好。
The real problem happens when you are working on an open-source Rails app that needs to have a configurable database adapter. I'm developing the Rails 3 branch of Fat Free CRM.
My preference is postgres, but we want the default database to be mysql2.
In this case,
Gemfile.lock
still needs be checked in with the default set of gems, but I need to ignore changes that I have made to it on my machine. To accomplish this, I run:and to reverse:
It is also useful to include something like the following code in your
Gemfile
. This loads the appropriate database adapter gem, based on your database.yml.I can't say if this is an established best practice or not, but it works well for me.
我和我的同事有不同的Gemfile.lock,因为我们使用不同的平台,windows和mac,而我们的服务器是linux。
我们决定删除repo中的Gemfile.lock并在git repo中创建Gemfile.lock.server,就像database.yml一样。然后在服务器上部署之前,我们使用 cap 部署钩子将 Gemfile.lock.server 复制到服务器上的 Gemfile.lock
My workmates and I have different Gemfile.lock, because we use different platforms, windows and mac, and our server is linux.
We decide to remove Gemfile.lock in repo and create Gemfile.lock.server in git repo, just like database.yml. Then before deploy it on server, we copy Gemfile.lock.server to Gemfile.lock on server using cap deploy hook
2021 年的简单答案:
Gemfile.lock 也应该在 Rubygems 的版本控制中。现在接受的答案是 11 岁。
这里有一些推理(从评论中挑选出来):
@josevalim https://github。 com/heartcombo/devise/pull/3147#issuecomment-52193788
@rafaelfranca https://github.com/rails/rails/pull/18951#issuecomment -74888396
Rails 在 git 中也有 Gemfile.lock:
Simple answer in the year 2021:
Gemfile.lock should be in the version control also for Rubygems. The accepted answer is now 11 years old.
Some reasoning here (cherry-picked from comments):
@josevalim https://github.com/heartcombo/devise/pull/3147#issuecomment-52193788
@rafaelfranca https://github.com/rails/rails/pull/18951#issuecomment-74888396
Also Rails has Gemfile.lock in git:
同意 r-dub 的观点,将其保留在源代码控制中,但对我来说,真正的好处是:
在相同的环境中进行协作(忽略 windows 和 linux/mac 的东西)。在 Gemfile.lock 之前,下一个安装该项目的人可能会看到各种令人困惑的错误,并责备自己,但他只是那个幸运的人,获得了超级 gem 的下一个版本,打破了现有的依赖关系。
更糟糕的是,这种情况发生在服务器上,除非受到纪律处分并安装确切的版本,否则会获得未经测试的版本。 Gemfile.lock 使这一点变得明确,并且它会明确告诉您您的版本不同。
注意:记住将内容分组,如:开发和:测试
Agreeing with r-dub, keep it in source control, but to me, the real benefit is this:
collaboration in identical environments (disregarding the windohs and linux/mac stuff). Before Gemfile.lock, the next dude to install the project might see all kinds of confusing errors, blaming himself, but he was just that lucky guy getting the next version of super gem, breaking existing dependencies.
Worse, this happened on the servers, getting untested version unless being disciplined and install exact version. Gemfile.lock makes this explicit, and it will explicitly tell you that your versions are different.
Note: remember to group stuff, as :development and :test
Bundler 文档也解决了这个问题:
原始: http://gembundler.com/v1.3 /rationale.html
编辑: http://web.archive.org/web/20160309170442/http://bundler.io/v1.3/rationale.html
请参阅“将代码签入版本控制”部分:
The Bundler docs address this question as well:
ORIGINAL: http://gembundler.com/v1.3/rationale.html
EDIT: http://web.archive.org/web/20160309170442/http://bundler.io/v1.3/rationale.html
See the section called "Checking Your Code into Version Control":
没有 Gemfile.lock 意味着:
如果您丢失了本地 Gemfile.lock , 始终检查 Gemfile.lock,如果您想更彻底,请让 travis 删除它 https://grosser.it/2015/08/14/check-in-your-gemfile-lock/
No Gemfile.lock means:
-> Always check in Gemfile.lock, make travis delete it if you want to be extra thorough https://grosser.it/2015/08/14/check-in-your-gemfile-lock/
来晚了一点,但答案仍然花了我时间和外国阅读来理解这个问题。所以我想总结一下我对 Gemfile.lock 的了解。
当您构建 Rails 应用程序时,您将在本地计算机中使用某些版本的 gem。如果您想避免生产模式和其他分支中的错误,则必须在各处使用该 Gemfile.lock 文件,并告诉捆绑器在每次更改时
bundle
重建 gem。如果
Gemfile.lock
在您的生产机器上发生了更改,并且 Git 不允许您git pull
,您应该将git reset --hard
写入避免文件更改并再次写入 git pull 。A little late to the party, but answers still took me time and foreign reads to understand this problem. So I want to summarize what I have find out about the Gemfile.lock.
When you are building a Rails App, you are using certain versions of gems in your local machine. If you want to avoid errors in the production mode and other branches, you have to use that one Gemfile.lock file everywhere and tell bundler to
bundle
for rebuilding gems every time it changes.If
Gemfile.lock
has changed on your production machine and Git doesn't let yougit pull
, you should writegit reset --hard
to avoid that file change and writegit pull
again.这里的其他答案是正确的:是的,您的 Ruby 应用程序(不是您的 Ruby gem)应该在存储库中包含
Gemfile.lock
。要详细了解为什么应该这样做,请继续阅读:我错误地认为每个环境(开发、测试、登台、产品...)都执行了
捆绑安装 构建自己的 Gemfile.lock。我的假设是基于 Gemfile.lock 不包含任何分组数据的事实,例如 :test、:prod 等。这个假设是错误的,正如我在一个痛苦的本地问题中发现的那样。
经过仔细调查,我很困惑为什么我的 Jenkins 构建显示成功获取特定的 gem(ffaker,FWIW),但是当应用程序加载并需要 ffaker 时,它说找不到文件。搞什么?
更多的调查和实验显示了这两个文件的作用:
首先,它使用 Gemfile.lock 来获取所有 gem,甚至是那些不会在该特定环境中使用的 gem。 然后它使用 Gemfile 来选择在这个环境中实际使用那些获取的 gem。
因此,尽管它在第一步中基于 Gemfile.lock 获取了 gem,但它并未包含在我的 :test 环境中(基于 Gemfile 中的组)。
修复(在我的例子中)是将 gem 'ffaker' 从 :development 组移动到主组,这样所有的环境都可以使用它。 (或者,根据情况仅将其添加到 :development、:test 中)
The other answers here are correct: Yes, your Ruby app (not your Ruby gem) should include
Gemfile.lock
in the repo. To expand on why it should do this, read on:I was under the mistaken notion that each env (development, test, staging, prod...) each did a
bundle install
to build their own Gemfile.lock. My assumption was based on the fact that Gemfile.lock does not contain any grouping data, such as :test, :prod, etc. This assumption was wrong, as I found out in a painful local problem.Upon closer investigation, I was confused why my Jenkins build showed fetching a particular gem (
ffaker
, FWIW) successfully, but when the app loaded and required ffaker, it said file not found. WTF?A little more investigation and experimenting showed what the two files do:
First it uses Gemfile.lock to go fetch all the gems, even those that won't be used in this particular env. Then it uses Gemfile to choose which of those fetched gems to actually use in this env.
So, even though it fetched the gem in the first step based on Gemfile.lock, it did NOT include in my :test environment, based on the groups in Gemfile.
The fix (in my case) was to move
gem 'ffaker'
from the :development group to the main group, so all env's could use it. (Or, add it only to :development, :test, as appropriate)2022 年更新,来自 TrinitronX
遗留答案 ~2010
假设您没有编写 rubygem,Gemfile.lock 应该位于您的存储库中。它用作所有所需 gem 及其依赖项的快照。这样,bundler 就不必在每次部署等时重新计算所有 gem 依赖项。
来自下面的牛仔编码的评论:
这是一篇不错的文章解释锁定文件是什么。
Update for 2022 from TrinitronX
Legacy answer ~2010
Assuming you're not writing a rubygem, Gemfile.lock should be in your repository. It's used as a snapshot of all your required gems and their dependencies. This way bundler doesn't have to recalculate all the gem dependencies each time you deploy, etc.
From cowboycoded's comment below:
Here's a nice article explaining what the lock file is.