git restore

在没有 --source 选项时,有 --staged 则默认使用 HEAD 来恢复,如果没有则默认使用 index。此时:

  • git restore 相当于 git restore --worktree
  • git restore --staged 用 HEAD 的信息来恢复 index,不会改动 working tree。
  • git restore --worktree 用 index 的信息来恢复 working tree。
  • git restore --worktree --staged 同时恢复 working tree 和 index。git restore --source HEAD --worktree --staged 相当于 git reset --hard 对于单个文件的效果。(虽然按照官方的描述来说是默认 source 为 HEAD,但是在 git 2.25.1 中实测这种情况下的默认 source 是 index)

-W link1; link2 link3 Specify the restore location. If neither option is specified, by default the working tree is restored. Specifying --staged will only restore the index. Specifying both restores both.

Torek insists on the fact git restore --source <aCommit> --worktree -- somefile is new (cannot be done with checkout): the ability to restore a file in the working tree without touching the index.Torek insists on the fact git restore --source <aCommit> --worktree -- somefile is new (cannot be done with checkout): the ability to restore a file in the working tree without touching the index.

这大概是说 git checkout 利用历史 commit 改动单个文件的同时也会改动 index。

git restore --staged file 相当于 git reset HEAD file。这是因为 git reset 默认是 --mixed 模式,只改变 index 但是不改变 working tree。