checkout命令经常被用来切换分支,但是git checkout -- fileName
还可以将没有提交到暂存区中的修改删除,恢复未修改的状态(但是对于新建的文件无法恢复到新建之前)
现在有两个文件,README.md 和 README2.md,进行修改,并新建文件README3.md,执行git status
之后
```
git$ git status
On branch master
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: README.md
modified: README2.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
README3.md
no changes added to commit (use "git add" and/or "git commit -a")
```
然后执行git add README.md
将第一个文件加入到暂存区,执行git status
命令如下
```
git$ git add README.md
git$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: README.md
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: README2.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
README3.md
```
现在执行git checkout -- .
(.
符号表示全部文件)全部恢复工作区修改之前的内容,再执行git status
命令如下
```
git$ git checkout -- .
git$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
README3.md
```
可见 git checkout -- file
可以恢复未被暂存的内容,但是不能恢复被暂存的内容和新建的文件,那么如果想要恢复已经暂存的内容怎么办,那么就需要用到git reset了