How do we understand git checkout [dot]

时间:2018-09-19 08:30:12

标签: git git-checkout

As my understanding, git checkout is about moving the head.

For example, git checkout <commit id> is to move the head to the <commit id>, git checkout <branch_name> is to move the head to the <branch_name>.

However, git checkout . is to discard the unstaged changes. it doesn't seem that there is any business about head.

So I can not understand why git uses the same key word checkout to do two totally no-relative things. Or git checkout . still works based on head?

2 个答案:

答案 0 :(得分:6)

您已经注意到,checkout命令被重载以表示两种不同的含义。我认为git help checkout可以很清楚地说明这一点:

git-checkout - Switch branches or restore working tree files

该命令有几种形式,您要询问的是:

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

在您的情况下,将省略<tree-ish>参数,而省略用于分隔选项和文件名的--参数,并且<pathspec>.(即当前目录) 。该命令的形式记录为:

  

通过替换索引或<tree-ish>(通常是一次提交)中的内容来覆盖工作树中的路径。

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

您可以将其视为“从存储库中检出这些文件”,这可能意味着提交或索引(索引可能包含已暂存但尚未提交的更改)。

答案 1 :(得分:1)

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

工作将取决于签出后的参数。如果是文件名,则将文件重置为“当前索引”。 从其他分支获取文件也很有用。

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