git恢复工作区删除的文件
首先,我们应该理解git中工作区、暂存区和版本库的概念,工作区为当前的工作目录,通过git add 命令可将其添加到暂存区,通过git commit 可将暂存区的文件添加到版本库中。
此时,我们向版本库中添加了readme.md这个文件,可通过git status查看工作区的状态:
git add readme.md
git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: readme.md
git commit -m 'first commit'
[master 83e38c7] added new readme.md
1 file changed, 5 insertions(+), 0 deletions(-)
如果此时我们在工作区删除了readme.md这个文件,使用git status发现:
git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout --<file>..." to discard changes in working directory
deleted: readme.md
此时,我们可以通过git checkout –readme.md,将该文件恢复到工作区中。