git 如何撤消对特定文件的本地更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31281679/
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
How to undo local changes to a specific file
提问by jww
I'm trying to undo local changes to a specific file. Nothing has been committed.
我正在尝试撤消对特定文件的本地更改。没有任何事情发生。
When I want to revert all changes, I can perform git revert --reset HEAD
. However, in this case, I don't want to revert all changes to all files.
当我想恢复所有更改时,我可以执行git revert --reset HEAD
. 但是,在这种情况下,我不想还原对所有文件的所有更改。
Its not clear or obvious to me how to revert just a file, even after reading the git-revert(3)
man page:
即使在阅读git-revert(3)
手册页之后,我也不清楚或不明显如何恢复文件:
NAME
git-revert - Revert some existing commits
SYNOPSIS
git revert [--[no-]edit] [-n] [-m parent-number] [-s] <commit>...
git revert --continue
git revert --quit
git revert --abort
...
This is similar to How to revert a specific file in a old commit on git, but no commits have been performed. And unlike OP, who wants to go back to an arbitrary commit, I just want the file to return to MASTER's copy of it.
这类似于如何在 git 上的旧提交中还原特定文件,但尚未执行任何提交。与想要返回任意提交的 OP 不同,我只希望文件返回到 MASTER 的副本。
Under SVN, I would just delete the file in question and then perform a svn update
.
在 SVN 下,我只会删除有问题的文件,然后执行svn update
.
How do I revert changes to a single file?
如何将更改还原到单个文件?
回答by mkasberg
You don't want git revert
. That undoes a previous commit. You want git checkout
to get git's version of the file from master.
你不想git revert
。这会撤消之前的提交。您想git checkout
从 master 获取文件的 git 版本。
git checkout -- filename.txt
git checkout -- filename.txt
In general, when you want to perform a git operation on a single file, use -- filename
.
一般来说,当你想对单个文件执行 git 操作时,使用-- filename
.
2020 Update
2020 更新
Git introduceda new command git restore
in version 2.23.0
. Therefore, if you have git version 2.23.0+
, you can simply git restore filename.txt
- which does the same thing as git checkout -- filename.txt
. The docs for this command do note that it is currently experimental.
Git在 version 中引入了一个新命令。因此,如果你有 git version ,你可以简单地- 这与. 此命令的文档确实指出它目前是实验性的。git restore
2.23.0
2.23.0+
git restore filename.txt
git checkout -- filename.txt