git: 从远程分支签出更改本地文件?

git: checking out from remote branch changes local files?

我刚刚检查了一个远程分支,直接在文件中查看与本地分支的差异。根本没有做任何更改,但是当我尝试返回 (ck = checkout) master 时,我收到错误消息:“您对以下文件的本地更改将被检出覆盖:

编辑:错误没有说明任何关于未决提交或任何内容

我不明白为什么签出会对文件进行任何更改,但是签出来自远程分支并且错误显示 "local files" 的事实让我感到困惑......然后本地文件有指的是我要切换到的本地主分支,但我刚刚提交了该主分支中的所有更改......(!?)。

编辑:这是提交后的状态:

我(肯定)不明白什么?

简答

放弃对您的工作树所做的任何更改,这些更改可能已添加到索引

git checkout HEAD -- .

对于 Windows FAT32 文件系统上的存储库,告诉 git 忽略文件模式上的执行位。

git config core.fileMode false

之后你应该可以检查你的主 b运行ch.

git checkout master

或使用您的别名

git ck master

详细说明

告诉 git 在当前目录 (.) 和任何子目录中添加所有新文件和更改后

git add .

然后提交,注意 git 告诉您文件权限已更改,例如

mode change 100644 => 100755 .gitignore

与其正上方的行合并

264 files changed, 0 insertions(+), 0 deletions(-)

我们得出结论,内容没有改变——只有权限。

本段的上下文很重要。紧接着 运行git commit -m 'many changes were not ...',您可以通过 运行ning [=] 确认这一点26=],输出将包含许多形式为

的序列
$ git diff origin/master
diff --git a/.gitignore b/.gitignore
old mode 100644
new mode 100755
[...]

但没有插入新行或删除行。

你的截图的外观运行ce 和一个名为 Desktop 的目录让我相信(你后来的评论证实了这一点)你在一个文件系统上使用 Microsoft Windows不代表执行模式位,因此必须告诉 git 使用 git config core.fileMode false 忽略它,git config documentation 对此进行了解释。

core.fileMode
Tells Git if the executable bit of files in the working tree is to be honored.

Some filesystems lose the executable bit when a file that is marked as executable is checked out, or checks out a non-executable file with executable bit on. git-clone or git-init probe the filesystem to see if it handles the executable bit correctly and this variable is automatically set as necessary.

A repository, however, may be on a filesystem that handles the filemode correctly, and this variable is set to true when created, but later may be made accessible from another environment that loses the filemode (e.g. exporting ext4 via CIFS mount, visiting a Cygwin created repository with Git for Windows or Eclipse). In such a case it may be necessary to set this variable to false. See git-update-index.

Git哲学

Git 非常小心不要破坏您的工作。在继续检查 master 之前,git 将您当前工作树的状态与您想要的 b运行ch 的头部进行比较,例如,看到 .gitignore 它位于与 master 中的 .gitignore 不同。

因为 git 看到它必须覆盖到结帐主机的未提交更改,它会中止结帐并让您知道您需要故意丢弃这些更改

git checkout HEAD -- .

或用 git stashgit add 后跟 git commit 将它们放在安全的地方。使用干净的工作树,您可以安全地检查不同的 b运行ches.

比较文件

有关具体更改内容的详细信息,运行以下命令之一。正确的调用取决于上下文。

创建新提交后

您添加了更改并在 b运行ch master 上创建了一个 SHA-1 对象名称为 644ccb8 的提交(如 git commit -m [...] 输出的屏幕截图所示)。在你创建它的时候,644ccb8 变成了你的 master b运行ch 的 HEAD,要查看它和原点 master 之间的所有变化,运行

git diff origin/master

在英语中,这意味着“显示 origin/master 和我目前正在处理的 b运行ch 的尖端之间的所有变化。”事实上,上面的命令等同于 git diff HEAD origin/master,它明确要求与 HEAD.

进行比较

要显示以 -- 分隔符命名的一个或多个文件的更改,请使用

git diff origin/master -- .gitignore README.md

暂存之后但提交之前

您的屏幕截图并未显示所有内容,但 git status 输出中的绿色文本表示某些更改已“暂存”或添加到索引中。在 git 中,我们通过一次或多次调用 git add 在索引(也称为暂存区或缓存)中逐步构建下一次提交,并且不太常见地从索引中删除更改git reset。一旦索引处于良好状态,git commit 将包含索引内容的新提交添加到历史记录中。

您的屏幕截图显示您从存储库的根目录 运行 git add .

  • 旁白:请注意 git add . 是用粗笔画的。有时这正是您想要的,但通常它会拾取比您预期更多的文件——尤其是当您的 .gitignore 缺少一些模式时。在大 git add 之后,一定要 运行 git status 以确保你没有搭上任何搭便车的人。

After 运行ning git add 一次或多次但 before 运行ning git commit, 查看当前的变化在索引或缓存中,添加 --cached 选项。

git diff --cached

或限制为一个或多个特定文件

git diff --cached -- README.md

分期前

要查看工作树中的内容与索引(或缓存)中的内容之间的差异,运行

之一
git diff
git diff -- README.md

这是git diff的默认模式。

分离头

从您的屏幕截图中可以看出,您的存储库有一个 detached HEAD,在文档中定义为

It means simply that HEAD refers to a specific commit, as opposed to referring to a named branch.

根据经验,您通常不想检查远程跟踪 b运行ches(例如origin/master)、标签或 SHA-1 散列,如果您打算进行修改。