restore :v. 恢复(某种情况或感受);使复原;使复位;

总结

区别:

git restore --staged <file>: 将文件从暂存区撤出,但不会撤销 工作区 文件的更改 (即: git status 提示的 被修改 且 被加入暂存区的内容,会被撤销,工作区文件的更改 不会变)
git restore  <file> :将不在暂存区的文件撤销更改 (即: git status 提示的 被修改 但 未被加入暂存区的内容,会被撤销)

git restore --staged <file>: 撤销 暂存区

git restore <file>: 撤销 本地修改

不懂:继续看下去

git restore
表示将在 工作区 但是 不在暂存区 的文件撤销更改


主角三个文件:application.yml 、logback-spring.xml、.gitignore
过              程:

  • application.yml 、logback-spring.xml ,两个文件使用git add 命令添加到了暂存区(在暂存区)
  • .gitignore 文件是修改过,但没有 git add 的文件(不在暂存区)
  • 使用git restore .gitignore 命令后,使用git status 查看文件状态,发现 .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   
                                                                          
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>                                   

 

git restore --staged

作用是将 暂存区的文件从暂存区撤出,但不会更改文件

主角三个文件:application.yml 、logback-spring.xml、.gitignore
过              程:

  • 演示完整的过程,从更改文件到添加到暂存区再到从暂存区撤出  (过程已经以“-------------”线分隔)
  • 初始状态:application.yml 、logback-spring.xml ,两个文件使用git add 命令添加到了暂存区(在暂存区)
  • 手动修改一下 .gitignore文件,再查看状态,.gitignore文件未添加到暂存区(不在暂存区)
  • 使用 git add .gitignore 将 .gitignore 文件添加到暂存区
  • 重点来了,我们使用git restore --staged 将.gitognore 文件存暂存区撤出
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>
-----------------------------------------------------------------------------------
	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>git add .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:   .gitignore
        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>
-------------------------------------------------------------------------------
E:\JavaDev\template_workspace\zhw-free>
E:\JavaDev\template_workspace\zhw-free>git restore --staged .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

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>

 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐