checkout

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

转载于:https://www.cnblogs.com/xiaohanblog/p/7831776.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值