git restore
git restore <file>

表示将在工作空间但是不在暂存区的文件撤销更改
示例:

E:\JavaDev\template_workspace\zhw-free>git status
On branch master
Your branch is up to date with 'origin/master'.                           Changes to be committed:                                                  (use "git restore --staged <file>..." to unstage)                       modified:   zhw-free-demo/src/main/resources/application.yml      new file:   zhw-free-demo/src/main/resources/logback-spring.xml   Changes not staged for commit:                                            (use "git add <file>..." to update what will be committed)              (use "git restore <file>..." to discard changes in working directory)   modified:   .gitignore                                            E:\JavaDev\template_workspace\zhw-free>
E:\JavaDev\template_workspace\zhw-free>
E:\JavaDev\template_workspace\zhw-free>
E:\JavaDev\template_workspace\zhw-free>
E:\JavaDev\template_workspace\zhw-free>
E:\JavaDev\template_workspace\zhw-free>
E:\JavaDev\template_workspace\zhw-free>git restore .gitignore             E:\JavaDev\template_workspace\zhw-free>git status
On branch master
Your branch is up to date with 'origin/master'.                           Changes to be committed:                                                  (use "git restore --staged <file>..." to unstage)                       modified:   zhw-free-demo/src/main/resources/application.yml      new file:   zhw-free-demo/src/main/resources/logback-spring.xml   E:\JavaDev\template_workspace\zhw-free>   

之前已经将 zhw-free-demo/src/main/resources/application.yml 和 zhw-free-demo/src/main/resources/logback-spring.xml 两个文件使用git add 命令添加到了暂存区,.gitignore 文件是修改过,但没有 git add 的文件(不在暂存区)。使用git restore .gitignore 命令后,使用git status 查看文件状态,发现.gitignore 文件的更改被撤销了。

git restore --staged
git restore --staged <file>

作用是将暂存区的文件从暂存区撤出,但不会更改文件
示例:
这里我们演示完整的过程,从更改文件到添加到暂存区再到从暂存区撤出初始状态

E:\JavaDev\template_workspace\zhw-free>git status
On branch master
Your branch is up to date with 'origin/master'.Changes to be committed:(use "git restore --staged <file>..." to unstage)modified:   zhw-free-demo/src/main/resources/application.ymlnew file:   zhw-free-demo/src/main/resources/logback-spring.xmlE:\JavaDev\template_workspace\zhw-free>

手动修改一下 .gitignore文件,再查看状态

E:\JavaDev\template_workspace\zhw-free>git status
On branch master
Your branch is up to date with 'origin/master'.Changes to be committed:(use "git restore --staged <file>..." to unstage)modified:   zhw-free-demo/src/main/resources/application.ymlnew file:   zhw-free-demo/src/main/resources/logback-spring.xmlChanges not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)modified:   .gitignoreE:\JavaDev\template_workspace\zhw-free>

使用 git add .gitignore 将 .gitignore 文件添加到暂存区

E:\JavaDev\template_workspace\zhw-free>git add .gitignoreE:\JavaDev\template_workspace\zhw-free>git status
On branch master
Your branch is up to date with 'origin/master'.Changes to be committed:(use "git restore --staged <file>..." to unstage)modified:   .gitignoremodified:   zhw-free-demo/src/main/resources/application.ymlnew file:   zhw-free-demo/src/main/resources/logback-spring.xmlE:\JavaDev\template_workspace\zhw-free>

重点来了,我们使用git restore --staged 将.gitognore 文件存暂存区撤出

E:\JavaDev\template_workspace\zhw-free>
E:\JavaDev\template_workspace\zhw-free>git restore --staged .gitignoreE:\JavaDev\template_workspace\zhw-free>git status
On branch master
Your branch is up to date with 'origin/master'.Changes to be committed:(use "git restore --staged <file>..." to unstage)modified:   zhw-free-demo/src/main/resources/application.ymlnew file:   zhw-free-demo/src/main/resources/logback-spring.xmlChanges not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)modified:   .gitignoreE:\JavaDev\template_workspace\zhw-free>
E:\JavaDev\template_workspace\zhw-free>
E:\JavaDev\template_workspace\zhw-free>

总结
git restore --staged 将文件从暂存区撤出,但不会撤销文件的更改
git resore 将不在暂存区的文件撤销更改
————————————————
版权声明:本文为CSDN博主「一醉自救」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u013493841/article/details/104451987/

git restore 和 git restore --staged 的区别相关推荐

  1. git reset, git status, git clean, .gitignore, git diff, git restore 的关系梳理

    当进入装有git的目录里,创建一文件,如 $touch test.txt 这时候,运行git status,就会发现这个文件出现在untracked files中 $git status On bra ...

  2. git: git add --ignore-removal git add --all 区别

    遇到的问题 在仓库中删除文件后,试图直接用 git add . 将所有删除工作提交暂存区,结果遇到了报错: $ git add . warning: You ran 'git add' with ne ...

  3. git init 与 git init --bare 区别

    git init 与 git init --bare 区别 发现问题 最早是在公司的wiki上发现了这个命令,google后发现值得记录下来 实践中发现的区别 网上找了很多资料,但说的很乱,干脆在自己 ...

  4. git:Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists)....

    Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists). 解决办法一:保 ...

  5. “ git commit”和“ git push”之间有什么区别?

    在我正在学习的Git教程中, git commit用于存储您所做的更改. git push什么用? #1楼 git commit记录您对本地存储库的更改. git push使用本地更改更新 远程存储库 ...

  6. git fetch和git pull之间的区别--转载

    原文地址:http://blog.csdn.net/a19881029/article/details/42245955 git fetch和git pull都可以用来更新本地库,它们之间有什么区别呢 ...

  7. git fetch和git pull的区别_Git实战(实验楼)学习笔记 实验2 基本用法(下)

    一.实验介绍 本节实验为 Git 入门第二个实验,继续练习最常用的 Git 命令. 知识点 对比差异 分布式的工作流程 Git 标签 实验环境 实验环境为 Ubuntu Linux 命令行环境,需要了 ...

  8. 详解git pull和git fetch的区别:

    前言 在我们使用git的时候用的更新代码是git fetch,git pull这两条指令.但是有没有小伙伴去思考过这两者的区别呢?有经验的人总是说最好用git fetch+git merge,不建议用 ...

  9. 玩转GIT系列之【git pull和git fetch的区别】

    大家都知道,git中从远程的分支获取最新的版本到本地有2个命令,git pull和git fetch.但是,这两个命令究竟有什么区别?使用时候该怎么选择呢?很多人不是很清楚,我自己也不是很清楚.今天就 ...

  10. Git rebase 和 Git merge 的区别,你知道吗?

    编辑搜图 请点击输入图片描述 Git是大多数程序必备的工具之一,Git常用那么几个命令:pull.push.status.merge.rebase.Git rebase 和 Git merge都是合并 ...

最新文章

  1. R in Action 读书笔记(5)
  2. Java关键字及其作用
  3. Python编程的若干个经典小技巧
  4. C语言普及组NOIP考试培训,NOIP 2018普及组复赛解题报告来了!
  5. linux下面拷贝pdf却没法在windows下面打开
  6. 对称密码的编程使用(DES、3DES、AES)
  7. Springboot 2.x 单元测试 JUnit 5
  8. java版 modbus crc16校验 (已测试成功)_java版 ModBus CRC16校验 (已测试成功)
  9. python __builtins__ frozenset类 (27)
  10. vue过滤器的那点事
  11. node.js中npm包管理工具
  12. unity 随机数_Unity 雨水滴到屏幕效果
  13. GdiPlus[45]: IGPGraphics (四) 关于呈现质量与合成模式
  14. 【拆机】小米路由器4
  15. 携程瘫痪非偶然 国内互联网企业漠视数据管理
  16. LeCun、Bengio、Hinton三巨头曾合体,Nature发文综述深度学习(论文精华)
  17. numpy: np.asarray 函数
  18. SEO为什么一定要面面俱到?
  19. 为什么业务中很少用到设计模式
  20. VR成为下一代计算平台的关键技术:面部追踪

热门文章

  1. 7月编程排行榜新鲜出炉,再次上演神仙打架!
  2. java 米转换公里_java中把米换算成公里的代码是什么?
  3. SWITCH 和ROUTE的故事(来自Csico论坛)
  4. [转] Photoshop教程8000例
  5. day26_安卓基础之之Android介绍与入门
  6. Linux远程操作(rcp/rsh)命令实战
  7. 大数据技术之Structured Steaming课程
  8. 3.3. debug ip igrp
  9. 2016年7月的数据省市区数据
  10. Kali Linux入门