错误:合并将覆盖以下未跟踪的工作树文件:

时间:2013-02-04 22:25:25

标签: git-merge railstutorial.org

提前感谢您的帮助!

我刚刚完成了Rails Tutorial的第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

1 个答案:

答案 0 :(得分:3)

错误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的副本。 merge命令使它覆盖已经在modeling-users分支内的文件的未跟踪版本。因此,错误消息警告您可能会丢失您想要保留的内容。

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

Unable to save ~/code/rails_projects/sample_app/spec/models/user_spec.rb消息似乎与此无关。错误消息不能做多少;也许在其他地方有更多的信息。

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