Git 放弃所有更改

Git discard all changes

大家好,我是正在学习的新开发人员。

我使用 git 桌面,如果您不想将该文件添加到提交中,您可以在其中右键单击一个文件并 select 放弃更改。它恢复到原来的状态。

现在我在终端工作,我怎么能在终端工作呢?

我错误地向文件中添加了一些更改,但我不想保留这些更改。请帮忙。

拜托,我不是高级用户,因此请尽可能简单。我还没有在那台机器上做任何提交。

我只对我用谷歌搜索感兴趣,但找不到简单的外行答案。

您可以简单地使用:

git checkout -- path/to/file

--使得文件名或路径无法推断为分支或remote/branch。

您可以通过多种方式做到这一点

  1. 您可以签出原始文件
git checkout file_path
  1. 隐藏更改并继续工作
git stash file_path
  1. 将 head 恢复到上一个​​提交
git reset --hard HEAD~1
  1. 签出特定提交
git reset --hard commit_sha

有多种方法可以做到这一点。我会提到最简单的。 它有 2 个步骤:

  1. Unstage the file to latest/current commit. Only required if the the changes to the file is staged.

    git reset HEAD <file>

  2. 撤消文件更改。

    git checkout <file>