git restore 和 git restore --staged 的區別(詳細)

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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章