checkout

时间:2017-11-14 13:02:08   收藏:0   阅读:215

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

原文:http://www.cnblogs.com/xiaohanblog/p/7831776.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!