【问题标题】:Git refusing to merge because of changes to an unchanged file由于更改了未更改的文件,Git 拒绝合并
【发布时间】:2013-02-16 08:12:18
【问题描述】:

我遇到了一个 git 问题,我想将一个分支合并到 master 中,但它拒绝合并,因为它认为我对文件进行了本地更改。执行状态显示没有任何更改,并且对文件进行差异化不会产生任何结果。这是终端输出:

$ git checkout ProductsAndContents
Switched to branch 'ProductsAndContents'

$ git status
# On branch ProductsAndContents
nothing to commit (working directory clean)

$ git checkout master
Switched to branch 'master'

$ git status
# On branch master
nothing to commit (working directory clean)

$ git merge ProductsAndContents
error: Your local changes to the following files would be overwritten by merge:
    tests/unit/src/models/BrandTests.js
Please, commit your changes or stash them before you can merge.
Aborting

任何帮助或建议将不胜感激!

【问题讨论】:

  • 您是否设法将该文件放入.git/info/exclude 文件或.gitignore 文件中?
  • this answer 中的配置选项有帮助吗?
  • 嗨 Andrew,将 core.trustctime 设置为 false 似乎已经做到了。感谢您为我指明正确的方向:)

标签: git merge git-merge


【解决方案1】:

感谢 Andrew Myers 对我最初的问题的评论,我发现将 core.trustctime 设置为 false 解决了这个问题。

git config core.trustctime false

关于 inode 更改时间不匹配的问题。我猜这是因为 repo 位于与我使用的文件系统不同的机器上的文件共享上。

感谢所有建议!

【讨论】:

    【解决方案2】:

    好吧,如果您确信自己不会丢失任何工作,请尝试以下操作:

    rm tests/unit/src/models/BrandTests.js
    git checkout -- tests/unit/src/models/BrandTests.js
    

    这应该重置任何让它认为它发生变化的机制。如果它不起作用,请使用其他可能的方法。

    【讨论】:

    • 感谢特雷夫的建议!我试过了,但不幸的是它仍然给出了同样的错误。好奇怪……
    • 我也遇到过这种情况,但似乎git stash 会拒绝查看已修改的文件。我手动备份它,检查它,并手动比较更改。一定有更好的方法......
    • @PaulLammertsma 不要忘记stash 只不过是另一个提交并且有自己的哈希。您可以像操作任何其他提交一样对其进行操作。这留下了许多其他选择。我刚刚发现上述方法是最快的。
    • @TrevNorris 这很奇怪;一切都很顺利。它需要删除文件,再次检查并回复更改以进行整理。
    【解决方案3】:

    我猜这是你的场景:

    $ git checkout ProductsAndContents
    Switched to branch 'ProductsAndContents'
    

    分支ProductsAndContents 包含文件tests/unit/src/models/BrandTests.js,因此上面的checkout 创建它和/或确保它与请求的特定提交是最新的。

    $ git status
    # On branch ProductsAndContents
    nothing to commit (working directory clean)
    

    您没有进行任何更改,并从一个干净的工作目录开始,所以这很好。

    $ git checkout master
    Switched to branch 'master'
    

    这可以确保master 上最新提交中包含的所有文件在工作目录中都是最新的。但是,它不会删除或更新不包含在 master 中的文件或被忽略的文件。在这里,我假设 master 目前不包含相关文件。

    $ git status
    # On branch master
    nothing to commit (working directory clean)
    

    同样,您没有进行任何修改。此处未将相关文件作为未跟踪文件提及这一事实可能意味着它被忽略(在.gitignore.git/info/exclude 中)。 master 不包含该文件,因此它不关心它是否存在。

    $ git merge ProductsAndContents
    error: Your local changes to the following files would be overwritten by merge:
        tests/unit/src/models/BrandTests.js
    Please, commit your changes or stash them before you can merge.
    Aborting
    

    在这里,您在另一个分支中合并的尝试想要引入master 当前没有的文件,这通常没问题。但是,该文件存在于您的工作目录中,git 不想丢弃不确定是否保留在存储库中的内容。正如其他地方所建议的那样,您可以(假设您知道它是安全的)只需删除该文件并重新尝试合并。

    根据您提供的信息,我不能 100% 确定您所拥有的信息;不过,您可以运行这两个命令来检查这种情况:

    git ls-files master -- tests/unit/src/models/BrandTests.js
    git ls-files ProductsAndContents -- tests/unit/src/models/BrandTests.js
    

    如果我的猜测是正确的,第一个命令不会显示文件存在,而第二个会。然后检查.gitignore.git/info/exclude 以查看它是否被忽略(对于.gitignore,您可能需要检查两个分支中每个分支上存在的版本)。

    【讨论】:

    • 谢谢!不幸的是,该文件在两个分支中,而不是在 .gitignore 或 .git/info/exclude 中。这是输出: $ git ls-files master -- tests/unit/src/models/BrandTests.js tests/unit/src/models/BrandTests.js $ git ls-files ProductsAndContents -- tests/unit/src/models/ BrandTests.js 测试/unit/src/models/BrandTests.js
    【解决方案4】:

    仅仅因为每个分支都没有要提交的内容并不意味着它们是相同的。我有兴趣使用

    查看这两个文件之间的差异
    git diff
    

    您可以使用

    恢复到已提交到 repo 的修订版
    git checkout -- tests/unit/src/models/BrandTests.js
    

    【讨论】:

    • Please, commit your changes or stash them before you can merge. 表示未提交的更改,未提交分支之间的差异
    • 确实没有要提交的更改,也没有未跟踪的文件。正如我所提到的,差异产生零输出。我很困惑。
    猜你喜欢
    • 2019-01-08
    • 2012-08-27
    • 1970-01-01
    • 2019-07-25
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    相关资源
    最近更新 更多