git 回顾(2)修正与还原

1 恢复删除的文件

(1)删除 index.html

rm  index.html
ls

 (2)使用 git checkout  命令恢复文件

git checkout 
git checkout -- index.html 

2 恢复 使用  git rm 删除的文件

(1)使用 git rm 删除文件

git   rm  index.html
ls

(2)尝试使用 git checkout 恢复,输入命令后会报错,原因是使用 git rm 后不仅删除了文件,同时将文件的删除信息加入了索引中去。

git checkout -- index.html

(3)使用 git reset 撤销删除

git  reset HEAD index.html
git checkout -- index.html
ls

 3 还原提交


当我们在修改文件之后发现了错误,或者出现了 bug ,但是此时文件我们已经提交上去了,这时候我们就不能使用 git checkout 来进行还原了。需要使用 git revert

(1)修改 index.html ,并进行提交

code index.html
git commit  -a -m "Porpusely overite the contents of  index.html"
git log -n1

 (2) 尝试使用 git checkout 来还原,但是发现文件并没有被还原

git checkout -- index.html
cat index.html

 (3)使用 git revert  ,其中 HEAD 参数 说明只需要撤销上次提交的更改,--no-edit 代表不需要为此行为添加  

git revert HEAD --no-edit
 git log -n1
git checkout -- index.html
cat index.html

 

原文地址:https://www.cnblogs.com/Assist/p/14121677.html