Windows下创建Ruby on Rails应用时遇Errno::ENOENT错误求助
Rails new命令报错Errno::ENOENT:找不到git config命令
问题详情
我是Ruby on Rails新手,使用Windows版RubyInstaller(3.3.0版本)安装了Ruby和Rails(7.1.3.2版本),并在CMD中确认版本无误。但执行rails new schedule_tweet命令创建新应用时,持续出现Errno::ENOENT错误,提示No such file or directory - git config init.defaultbranch。我已确认Ruby安装目录下的app_base.rb文件存在,尝试手动添加Ruby至环境变量Path、重装Ruby及更换Ruby版本,均未解决问题。
错误栈
C:\>rails new schedule_tweet create create README.md create Rakefile create .ruby-version create config.ru create .gitignore create .gitattributes create Gemfile C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/generators/app_base.rb:733:in ``': No such file or directory - git config init.defaultbranch (Errno::ENOENT) from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/generators/app_base.rb:733:in `user_default_branch' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/generators/app_base.rb:737:in `git_init_command' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/generators/rails/app/app_generator.rb:25:in `method_missing' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/generators/rails/app/app_generator.rb:87:in `version_control' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/generators/app_base.rb:150:in `public_send' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/generators/app_base.rb:150:in `build' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/generators/rails/app/app_generator.rb:346:in `create_root_files' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor/command.rb:28:in `run' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor/invocation.rb:127:in `invoke_command' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor/invocation.rb:134:in `block in invoke_all' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor/invocation.rb:134:in `each' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor/invocation.rb:134:in `map' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor/invocation.rb:134:in `invoke_all' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor/group.rb:232:in `dispatch' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor/base.rb:584:in `start' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/commands/application/application_command.rb:28:in `perform' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor/command.rb:28:in `run' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor/invocation.rb:127:in `invoke_command' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/command/base.rb:178:in `invoke_command' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor.rb:527:in `dispatch' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/command/base.rb:73:in `perform' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/command.rb:71:in `block in invoke' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/command.rb:149:in `with_argv' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/command.rb:69:in `invoke' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/cli.rb:21:in `<top (required)>' from <internal:C:/Ruby33-x64/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:127:in `require' from <internal:C:/Ruby33-x64/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:127:in `require' from C:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/exe/rails:10:in `<top (required)>' from C:/Ruby33-x64/bin/rails:32:in `load' from C:/Ruby33-x64/bin/rails:32:in `<main>'
解决方案
方案1:跳过Git初始化
执行rails new命令时添加--skip-git参数,直接跳过Git仓库初始化步骤:rails new schedule_tweet --skip-git方案2:安装Git并配置环境变量
- 下载Windows版Git并完成安装;
- 安装时确保勾选“Add Git to PATH”选项;
- 重启CMD,执行
git --version确认Git可用; - 再次尝试
rails new schedule_tweet命令。
方案3:手动配置Git默认分支(可选)
若安装Git后仍有问题,先手动设置Git默认分支:git config --global init.defaultbranch main之后再执行
rails new命令。
内容的提问来源于stack exchange,提问作者N. Huy
相关产品推荐
相关产品推荐

