在Git中撤销对文件的修改,可以使用git checkout命令。
git checkout 的用法如下:
- git checkout — 撤销工作区对该文件所做的修改
- git checkout — 撤销该文件在某次提交之后的所做修改
例如:
修改readme.txt文件:
$ git status
On branch master
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: readme.txt
撤销对readme.txt的修改:
$ git checkout -- readme.txt
现在文件恢复到上次提交后的状态:
$ git status
On branch master
nothing to commit, working tree clean
假如又修改了文件,现在想撤销第3次提交之后的修改:
$ git log --oneline
28ea823 (HEAD -> master) Modify readme
c466f1a Modify readme
a13b91e Add readme
可以使用git checkout c466f1a — readme.txt撤销第3次提交之后的修改:
$ git checkout c466f1a -- readme.txt
现在文件恢复到第2次提交后的状态:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt