git 使用git,如何将工作树重置为索引的状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3107789/
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
Using git, how do you reset the working tree to the state of the index?
提问by haydenmuhl
Situation:
情况:
- Edit files
- Add files to the index
- Edit more files
- 编辑文件
- 将文件添加到索引
- 编辑更多文件
Now we have three different states. The state of HEAD, the state of the index and the state of the working tree. What is the command to undo changes in the working tree so that it matches the state of the index?
现在我们有三个不同的状态。HEAD 的状态、索引的状态和工作树的状态。撤消工作树中的更改以使其与索引状态匹配的命令是什么?
回答by CB Bailey
I tend to use git checkout .
which discards all changes from the working directory down. This makes a difference if you're not at the root of the repository.
我倾向于使用git checkout .
which 丢弃从工作目录向下的所有更改。如果您不在存储库的根目录下,这会有所不同。
This command doesn't remove newly created files which is usually a good thing. If you need to do this then you can use git clean
as well.
此命令不会删除新创建的文件,这通常是一件好事。如果您需要这样做,那么您也可以使用git clean
。
回答by Greg Hewgill
You can use git stash save --keep-index
to do this. After saving the stash, you can use git stash drop
if you don't want to keep it around.
您可以使用它git stash save --keep-index
来执行此操作。保存存储后,git stash drop
如果您不想保留它,可以使用。
回答by Peter Tillemans
You can use git-checkout-index. Be aware that you need to add
您可以使用git-checkout-index。请注意,您需要添加
-f
to force it to overwrite existing files, or-f -a
to enforce overwriting all paths in the index.
-f
强制它覆盖现有文件,或-f -a
强制覆盖索引中的所有路径。
回答by musiphil
git checkout :/
discards all changes in the working tree and replaces it with what's in the index, regardless of the current working directory.
git checkout :/
丢弃工作树中的所有更改并将其替换为索引中的内容,而不管当前工作目录如何。
https://git-scm.com/docs/gitglossary#gitglossary-aiddefpathspecapathspec
https://git-scm.com/docs/gitglossary#gitglossary-aiddefpathspecapathspec