比如Readme文件,修改其中内容,运行git status
$ git status
On branch master
Your branch is up to date with 'origin/master'.
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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
现在运行git add 将readme.md放到暂存区,然后再看看git status的输出:
$ 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: README.md
现在文件已经暂存,下次提交时就会记录到仓库。假设此时,再修改readme.md,运行git stastus看看:
$ 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: README.md
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: README.md
并且运行git status -s,可以得到一种更为紧凑的输出
$ git status -s
MM README.md
输出中标记位有2栏,左边指明了暂存区的状态,右栏指明了工作区的状态。表明readme文件已暂存,暂存后又做了修改。
那么如何撤销第二次的修改?
问题的本质,暂存区的结构
根据上文分析
暂存区中应该有add后的文件快照,暂存区存储的是add后的文件快照和这些快照的索引文件