使用 git 覆盖文件检出不会警告丢失更改

Override a file using git checkout does not warn about losing changes

我最近发现我可以覆盖我的跟踪远程分支中的文件来执行下一步:"git checkout origin/remoteBranchName nameFile.txt"。但我有一些问题:

  1. 由于未提交此文件中的更改,执行该命令不会警告丢失更改,而在切换到其他分支时会发出警告。为什么?

  2. 我尝试使用硬重置来覆盖,但我认为您无法使用一个文件来完成。为什么?

谢谢

I tried to override using hard reset but I think you cannot do it with one file. Why?

 $ git reset -- file.c 

根据https://git-scm.com/docs/git-reset

Reset a single file in the index

Suppose you have added a file to your index, but later decide you do not want to add it to your commit. You can remove the file from the index while keeping your changes with git reset.

$ git reset -- frotz.c (1)

$ git commit -m "Commit files in index" (2)

$ git add frotz.c (3)

  1. This removes the file from the index while keeping it in the working directory.

  2. This commits all other changes in the index.

  3. Adds the file to the index again.