首先我初始化一个仓库,并且提交了一个文件:
情景一
在工作区对index.html做了一些修改,还没有add到暂存区,并且暂存区没有index.html的修改。
我们先来执行 git status
来查看下状态:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
这里git告诉你可以使用命令git checkout -- <file>...
可以丢弃工作区的修改(目前还在工作区),对应的GitKraken上的操作为:
执行操作之后你会发现,之前工作区的修改已经没了,现在index.html回到了和版本库一样的状态。
情景二
对index.html做了一次更改,然后add到暂存区,接着又对它做了更改
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.html
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: index.html
此时我们执行:git checkout --index.html
就会撤销工作区的更改,回到和暂存区一样的状态:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.html