Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将git文件还原为其暂存区版本?_Git - Fatal编程技术网

如何将git文件还原为其暂存区版本?

如何将git文件还原为其暂存区版本?,git,Git,假设我有一个名为a.txt的文件。我将其添加到临时区域,然后修改它。我怎样才能恢复添加时的状态? Git 2.23之前的版本:Git checkout a.txt 从Git 2.23开始:Git restore a.txt 如果键入Git status,Git会告诉您这一点 Git 2.23之前的版本: # On branch master # Changes to be committed: # (use "git reset HEAD <file>..."

假设我有一个名为
a.txt
的文件。我将其添加到临时区域,然后修改它。我怎样才能恢复添加时的状态?

  • Git 2.23之前的版本:
    Git checkout a.txt
  • 从Git 2.23开始:
    Git restore a.txt
如果键入
Git status
,Git会告诉您这一点

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
#
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重置磁头…”取消分级)
#
#修改:a
#
#已更改但未更新:
#(使用“git add…”更新将提交的内容)
#(使用“git签出--…”放弃工作目录中的更改)
#
#修改:a
#
从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
#
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还原--暂存…”取消暂存)
修改:a
未为提交而暂存的更改:
(使用“git add…”更新将提交的内容)
(使用“git restore…”放弃工作目录中的更改)
修改:a
git checkout--a.txt

本页上的另一个答案没有
--
,导致了一些混乱

这是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
#
分支主机上的
#
#要提交的更改:
#(使用“git重置磁头…”取消分级)
#
#修改:a
#
#已更改但未更新:
#(使用“git add…”更新将提交的内容)
#(使用“git签出--…”放弃工作目录中的更改)
#
#修改:a
#
取消分级文件的分级

接下来的两部分将演示如何使用临时区域和工作目录更改。很好的一点是,用于确定这两个区域的状态的命令还提醒您如何撤消对它们的更改。例如,假设您更改了两个文件,并希望将它们作为两个单独的更改提交,但您意外地键入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
$git add*
$git状态
论分行行长
要提交的更改:
(使用“git重置磁头…”取消分级)
重命名:README.md->README
修改:contribution.md
在“要提交的更改”文本的正下方,它说使用git重置头。。。拆开。因此,让我们使用该建议来取消显示contribution.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
$git reset HEAD.md
重置后未老化的更改:
医学博士
$git状态
论分行行长
要提交的更改:
(使用“git重置磁头…”取消分级)
重命名:README.md->README
未为提交而暂存的更改:
(使用“git add…”更新将提交的内容)
(使用“git签出--…”放弃工作目录中的更改)
修改:contribution.md

这个命令有点奇怪,但它可以工作。“contribution.md”文件已修改,但再次未格式化。

@Daenyth我在发布之前已检查过它,您可以看到输出显示了在不同状态下重置文件的不同方式(分段与未分段)@Daenyth--您正在考虑“git签出分支名称路径”或“git签出头路径”@William:谢谢!现在更有意义了。不适用于新文件,所以它不会真正从暂存中签出,因为它确实需要一个对象。如何从暂存签出?编辑它确实可以像status所说的那样使用
-
。你最好告诉我们区别,而不是发布以前引用过的内容。