我如何在 git 中将一个文件恢复到其原始状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16951416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:36:04  来源:igfitidea点击:

How do i revert one file to its original state in git?

git

提问by u_ser722345

I tried searching a lot but I couldn't figure it out. I have a few files I made changes to.

我尝试搜索了很多,但我无法弄清楚。我对一些文件进行了更改。

Then I committed it and realized I made a change to a file I didn't want to change. How can I get the file back to its original state? I am on a branch. git checkout --filedidnt do anything at all.

然后我提交了它并意识到我对一个我不想更改的文件进行了更改。如何将文件恢复到原始状态?我在一个分支上。 git checkout --file根本没有做任何事情。

回答by amalloy

git checkout filename

will work in most cases, unless you have a branch with the same name as a file. In either case,

将在大多数情况下工作,除非您有一个与文件同名的分支。在任一情况下,

git checkout -- filename

will be sufficient.

就足够了。

回答by Johnsyweb

You need a space between --and your filename:

--和文件名之间需要一个空格:

git checkout -- filename

回答by dyng

Try

尝试

git checkout HEAD~ filename

instead.

反而。