你如何将一个 git 文件恢复到它的暂存区版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3044684/
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 do you revert a git file to its staging area version?
提问by Geo
Let's say I have a file named a.txt
. I add it to the staging area, and then I modify it. How could I return it to the way it was when I added it?
假设我有一个名为a.txt
. 我将它添加到暂存区,然后我修改它。我怎么能把它恢复到我添加它时的样子?
采纳答案by abyx
git checkout a.txt
git checkout a.txt
Git tells you this if you type git status
:
如果您键入git status
以下内容,Git 会告诉您:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a
#
回答by nonrectangular
git checkout -- a.txt
git checkout -- a.txt
The other answer on this page doesn't have the --
, and resulted in some confusion.
此页面上的另一个答案没有--
,并导致了一些混乱。
This is what Git tells you when you type git status
:
这是 Git 在您键入时告诉您的信息git status
:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a
#
回答by Girish Rathi
Unstaging a Staged File
取消暂存文件
The next two sections demonstrate how to work with your staging area and working directory changes. The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them. For example, let's say you've changed two files and want to commit them as two separate changes, but you accidentally type git add * and stage them both. How can you unstage one of the two? The git status command reminds you:
接下来的两节演示了如何处理临时区域和工作目录更改。好的部分是,您用来确定这两个区域状态的命令还会提醒您如何撤消对它们的更改。例如,假设您更改了两个文件并希望将它们作为两个单独的更改提交,但您不小心键入了 git add * 并将它们都暂存。你怎么能取消两者之一?git status 命令提醒您:
$ git add *
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
modified: CONTRIBUTING.md
Right below the “Changes to be committed” text, it says use git reset HEAD ... to unstage. So, let's use that advice to unstage the CONTRIBUTING.md file:
在“要提交的更改”文本的正下方,它说使用 git reset HEAD ... 取消暂存。因此,让我们使用该建议取消暂存 CONTRIBUTING.md 文件:
$ git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: CONTRIBUTING.md
The command is a bit strange, but it works. The CONTRIBUTING.md file is modified but once again unstaged.
该命令有点奇怪,但它有效。CONTRIBUTING.md 文件已修改但再次取消暂存。