运行 'git checkout HEAD .' 在错误的目录中

Ran 'git checkout HEAD .' in the wrong directory

糟糕,我不小心 运行 'get checkout HEAD .' 在错误的目录中。 它产生了输出 'Updated 2 paths from 2096756f' 但我不知道我的本地更改是什么。 我假设我找不到,但我想知道我以后如何保护自己,因为这个命令是我习惯性地丢弃本地更改的方式。有没有我可以为此创建的别名,以防止我将来犯这样的错误?

I accidentally ran 'get checkout HEAD .' in the wrong directory.

这很糟糕。

It produced the output 'Updated 2 paths from 2096756f' but I have no idea what my local changes were.

除非您有本地备份(zfs 快照、macOS Time Machine 等),否则它们就消失了。 Git 无法帮助您取回它们:您告诉 Git 从您的工作树中丢弃它们,它确实做到了。

I assume that I can't find out,

如果您的 OS 保持文件的修改时间,请查找两个 most-recently-modified 文件。这些将是 git checkout HEAD . 覆盖的两个。

I am wondering how I can protect myself in the future as this command is how I habitually discard local changes.

改变你的习惯。您不能为内置 Git 命令设置别名。您可以创建一个git别名或您自己的git命令:

  1. 检查您是否 运行 某些“安全”的东西,或者检查您是否正在使用此特定命令;
  2. 如果你是 运行 你认为“安全”的东西,运行带有相同参数的真正的 Git 命令,但如果你是 运行 git checkout HEAD ., 抱怨但没有这样做。

由于您是自己编写此 command-or-alias,因此您可以选择第 2 步的“但是”部分中的内容。这(大概)会促使您改变习惯。

(这些天我尝试使用 git status 然后 git restore -- path/to/file,这样我就知道我正在恢复什么;我可以 cut-and-paste 来自 git status 在这里。)

Is there some alias I can create that for this that would protect me from such a mistake in the future

没有。 checkout 很危险,Git 不会保护你自己。

但是一个很好的保护就是放弃使用checkout。而是使用 switchrestore。它们使犯下有害错误变得更加困难。