git - checkout 到另一个分支时,文件被删除为未暂存的更改

标签 git

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

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 diff between folders

最佳答案

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

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

$ 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")

The warning was added in 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 后,提交。

关于git - checkout 到另一个分支时,文件被删除为未暂存的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57275916/

相关文章:

git - 如何获取整个git历史记录中每个文件的大小?

不支持 Visual Studio 中的 Git 同步返回 URI 格式

git - 在 golang 中导入私有(private)存储库时版本和 https 引用无效

git - 将一个分支 merge 到 master 但限制你的提交

git - 尝试在 WSL 上使用 gpg 在 git 上签署提交但不起作用

java - 通过 JGit 添加远程

r - 如何以相对方式使用 setwd?

git - 忽略 .gitignore 中的行

git - 提交机器特定的配置文件

我是错误 : “error: while searching for:”