假设我有一个名为a.txt
的文件。我将它添加到临时区域,然后我修改它。我怎么能把它恢复到我添加它的时候呢?
答案 0 :(得分:61)
git checkout a.txt
如果您输入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
#
答案 1 :(得分:25)
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
#
答案 2 :(得分:0)
取消暂存文件
接下来的两节演示了如何使用暂存区和工作目录更改。令人高兴的是,用于确定这两个区域的状态的命令还提醒您如何撤消对它们的更改。例如,假设您已经更改了两个文件,并想将它们提交为两个单独的更改,但是您不小心键入了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 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
该命令有点奇怪,但是可以使用。已修改CONTRIBUTING.md文件,但再次取消登台。