【问题标题】:error: The following untracked working tree files would be overwritten by merge:错误:以下未跟踪的工作树文件将被合并覆盖:
【发布时间】:2013-02-04 22:25:24
【问题描述】:

提前感谢您的帮助!

我刚刚完成 Rails 教程的第 6 章:

http://ruby.railstutorial.org/chapters/modeling-users#sec-6_4

我跑了:

❤ git checkout master

一切顺利,然后我跑了:

❤ git merge modeling-users

并得到以下错误:

Updating fc9f72a..90d1ba6
error: The following untracked working tree files would be overwritten by merge:
        app/models/user.rb
Please move or remove them before you can merge.
Aborting

这是我的 app/models/user.rb 文件:

# == Schema Information
#
# Table name: users
#
#  id         :integer          not null, primary key
#  name       :string(255)
#  email      :string(255)
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }

  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true
end

现在我的 Sublime Text 2 user_spec.rb 和 application_helper_spec.rb 文件不会保存并出现以下错误:

无法保存 ~/code/rails_projects/sample_app/spec/models/user_spec.rb

user_spec.rb:

# == Schema Information
#
# Table name: users
#
#  id         :integer          not null, primary key
#  name       :string(255)
#  email      :string(255)
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

require 'spec_helper'

describe User do

  before do
    @user = User.new(name: "Example User", email: "user@example.com", 
                     password: "foobar", password_confirmation: "foobar")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  it { should respond_to(:authenticate) }

  it { should be_valid }

  describe "when name is not present" do
    before { @user.name = " " * 51 }
    it { should_not be_valid }
  end

  describe "when email format is valid" do
    it "should be valid" do
      addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@bax.cn]
      addresses.each do |valid_address|
        @user.email = valid_address
        @user.should be_valid
      end
      end
  end

  describe "when email format is valid" do
    it "should be valid" do
      addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      addresses.each do |valid_address|
        @user.email = valid_address
        @user.should be_valid
      end      
    end
  end

  describe "when email address is already taken" do
    before do
      user_with_same_email = @user.dup
      user_with_same_email.email = @user.email.upcase
      user_with_same_email.save
    end

    it { should_not be_valid }
  end

  describe "when password is not present" do
    before { @user.password = @user.password_confirmation = " " }
    it { should_not be_valid }
  end

  describe "when password doesn't match confirmation" do
    before { @user.password_confirmation = "mismatch" }
    it { should_not be_valid }
  end

  describe "when password confirmation is nil" do
    before { @user.password_confirmation = nil }
    it { should_not be_valid }
  end

  describe "with a password that's too short" do
    before { @user.password = @user.password_confirmation = "a" * 5 }
    it { should be_invalid }
  end

  describe "return value of authenticate method" do
    before { @user.save }
    let(:found_user) { User.find_by_email(@user.email) }

    describe "with valid password" do
      it { should == found_user.authenticate(@user.password) }
    end

    describe "with invalid password" do
      let(:user_for_invalid_password) { found_user.authenticate("invalid") }

      it { should_not == user_for_invalid_password }
      specify { user_for_invalid_password.should be_false }
    end
  end
end

还有我的 application_helper_spec.rb 文件:

require 'spec_helper'

describe ApplicationHelper do

  describe "full_title" do
    it "should include the page title" do
      full_title("foo").should =~ /foo/
    end

    it "should include the base title" do
      full_title("foo").should =~ /^Ruby on Rails Tutorial Sample App/
    end

    it "should not include a bar for the home page" do
      full_title("").should_not =~ /\|/
    end
  end
end

【问题讨论】:

    标签: git-merge railstutorial.org


    【解决方案1】:

    错误The following untracked working tree files would be overwritten by merge 正在发生,因为文件夹中有一个app/models/user.rb 文件,该文件当前未添加到任何分支。

    您可以通过运行git status 命令来验证这一点;它应该显示类似于以下的输出:

    $ git status
    # On branch master
    #
    # Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
    #
    #   app/models/user.rb
    no changes added to commit (use "git add" and/or "git commit -a")
    

    当您运行git merge modeling-users 时,它会尝试将modeling-users 分支合并到master 分支。 modeling-users 分支中已经包含 app/models/user.rb 的副本。合并命令使它用modeling-users 分支中已经存在的文件覆盖文件的未跟踪版本。因此,错误消息会警告您可能会丢失您想要保留的东西。

    正如git status 消息所示,通过运行git add app/models/user.rb 命令将文件添加到主分支。

    Unable to save ~/code/rails_projects/sample_app/spec/models/user_spec.rb 消息似乎与此无关。从错误消息中无法得出什么结论;也许其他地方有更多信息。

    关闭项目并重新打开它可能会有所帮助。您可能还想尝试将文件保存在不同的编辑器中(也许是 vim?)

    【讨论】:

      猜你喜欢
      • 2017-06-09
      • 2012-12-23
      • 2016-07-27
      • 2012-06-01
      • 2014-10-13
      • 1970-01-01
      • 2013-08-02
      • 2021-12-02
      • 2011-06-18
      相关资源
      最近更新 更多