简体   繁体   English

如何将 git 文件恢复为其暂存区版本?

[英]How do you revert a git file to its staging area version?

Let's say I have a file named a.txt .假设我有一个名为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?我怎样才能将它恢复到我添加它时的状态?

  • Prior to Git 2.23: git checkout a.txt在 Git 2.23 之前: git checkout a.txt
  • Starting from Git 2.23: git restore a.txt从 Git 2.23 开始: git restore a.txt

Git tells you this if you type git status .如果您键入git status Git 会告诉您这一点。

Prior to Git 2.23:在 Git 2.23 之前:

# 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
#

Starting from Git 2.23:从 Git 2.23 开始:

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   a

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   a

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 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
#

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.例如,假设您更改了两个文件并希望将它们作为两个单独的更改提交,但您不小心键入了 git add * 并将它们都暂存。 How can you unstage one of the two?你怎么能取消两者之一? The git status command reminds you: 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.在“要提交的更改”文本的正下方,它说使用 git reset HEAD ... 取消暂存。 So, let's use that advice to unstage the CONTRIBUTING.md file:因此,让我们使用该建议来取消暂存 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 文件已修改但再次取消暂存。

git restore --staged filename git restore --staged 文件名

This is the command to unstage a file after adding it.这是添加文件后取消暂存文件的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM