我们如何理解git结帐[点]

How do we understand git checkout [dot]

据我了解,git checkout 是关于移动 head

例如git checkout <commit id>就是把head移到<commit id>git checkout <branch_name>就是把head移到<branch_name>.

但是,git checkout . 是丢弃未暂存的更改。 head.

好像没什么生意

所以我不明白为什么git使用同一个关键字checkout来做两件完全没有关系的事情。或者 git checkout . 仍然基于 head?

您可以使用 git checkout 来更改您的分支名称以及重置文件(到远程)或特定分支中的文件。

工作将取决于结帐后的参数。如果它是一个文件名,它会将文件重置为当前索引中的文件。 这对于从其他分支获取文件也很有用。

如果它是一个分支名称,它将改变当前的工作分支。

如您所见,checkout 命令被重载以表示两种不同的含义。不过,我认为 git help checkout 说得很清楚了:

git-checkout - Switch branches or restore working tree files

命令有多种形式,您要问的是:

git checkout [<tree-ish>] [--] <pathspec>...

在你的例子中省略了 <tree-ish> 参数,省略了分隔选项和文件名的 -- 参数,并且 <pathspec>. (即当前目录)。该命令形式记录为:

Overwrite paths in the working tree by replacing with the contents in the index or in the <tree-ish> (most often a commit).

因此,由于您没有指定 <tree-ish> 参数,因此 . 中的文件内容将替换为索引中的内容。这意味着丢弃 . 中尚未添加到索引中的所有更改。

您可以将其视为 "checkout these files from the repository",这可以表示来自提交,也可以来自索引(可能包含已暂存但尚未提交的更改)。