首页 > 解决方案 > 签出到另一个分支时,文件作为未暂存的更改被删除

问题描述

合并后,我面临一个奇怪的问题,即签出另一个分支会导致未暂存的更改 - 删除一些文件。

PS>git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
PS>git checkout master_test
Switched to branch 'master_test'
Your branch is up to date with 'origin/master_test'.
PS>git status
On branch master_test
Your branch is up to date with 'origin/master_test'.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    Modules/CimCmdlets/CimCmdlets.psd1
        ...

由于它仅在我这样做时才会发生git checkout,因此我假设文件是​​由 git 本身删除的。如何确定这些文件被删除的原因?

编辑:这些分支上的文件之间没有区别,包括名称。例如: WinMerge 文件夹之间的差异

标签: git

解决方案


由于 Windows 默认情况下不区分大小写,因此您可能有两个名称相同但大小写不同的文件。WinMerge 等 Windows 工具无法发现这一点,因为这两个文件中只有一个被签出。

我将在默认情况下不区分大小写的 OS X 上演示这一点。我在区分大小写的卷上创建了一个 Git 存储库,其中包含FOOfoo文件。现在我正在一个不区分大小写的文件系统上检查它。

$ git clone /Volumes/case_sensitive/tmp/test
Cloning into 'test'...
done.
warning: the following paths have collided (e.g. case-sensitive paths
on a case-insensitive filesystem) and only one from the same
colliding group is in the working tree:

  'FOO'
  'foo'

$ ls
.git  foo

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   FOO

no changes added to commit (use "git add" and/or "git commit -a")

警告是在 2.20.0 中添加的

您可以使用git ls-files.

$ git ls-files
FOO
foo

我们可以通过将一个文件重命名为独特的东西来解决这个问题......

$ git mv foo foo2

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    renamed:    foo -> foo2

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    deleted:    FOO

现在它已经不碍事了,我们可以签出另一个文件。

$ git co FOO
Updated 1 path from the index

$ ls
.git  FOO  foo2

$ git st
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    renamed:    foo -> foo2

在你修复foo了以前使用的任何代码之后foo2,提交。


推荐阅读