【git】git restore 和 git restore --staged 的区别

【git】git restore 和 git restore --staged 的区别

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>

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

  1. 初始状态
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>
  1. 手动修改一下 .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>	
  1. 使用 git add .gitignore.gitignore 文件添加到暂存区
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>
  1. 重点来了,我们使用git restore --staged.gitognore 文件存暂存区撤出
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>

总结

git restore --staged 将文件从暂存区撤出,但不会撤销文件的更改
git resore 将不在暂存区的文件撤销更改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值