git restore 和 git restore --staged 的区别
git restore
git restore
表示将在工作空间但是不在暂存区的文件撤销更改
示例:
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 ..." 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 ..." to update what will be committed)
(use "git restore ..." 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 ..." 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
作用是将暂存区的文件从暂存区撤出,但不会更改文件
示例:
这里我们演示完整的过程,从更改文件到添加到暂存区再到从暂存区撤出
- 初始状态
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 ..." 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>
- 手动修改一下 .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 ..." 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 ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: .gitignore
e:\javadev\template_workspace\zhw-free>
- 使用
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 ..." 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>
- 重点来了,我们使用
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 ..." 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 ..." to update what will be committed)
(use "git restore ..." 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
将不在暂存区的文件撤销更改