API使用介绍
git restore <filename>撤销 工作区的内容git restore --staged <filename> 撤销 暂存区的内容回退到工作区
案例演示
演示从暂存区撤销内容
创建一个项目,新建readme.txt文件。新建readme.txt文件
# log查看提交历史记录,进行了4次提交shuais-MacBook-Pro:restore shuai$ git log --onelined9531bd (HEAD -> master) add file4cff528 v3bdaa5d3 v22688ed0 v1
然后又在readme.txt文件中增加一行
# 1.对文件readme新增一行内容,这是错误内容。操作目的是从暂存区撤销,并在本地也撤销shuais-MacBook-Pro:restore shuai$ echo "this is wrong! " >> readme.txt# 2.将内容添加到暂存区。【因为这时还不知道是错的内容】shuais-MacBook-Pro:restore shuai$ git add .# 3.查看文件状态,以及提交到了暂存区shuais-MacBook-Pro:restore shuai$ git statusOn branch masterChanges to be committed:(use "git restore --staged <file>..." to unstage)modified: readme.txt# 4.通过restore --staged可以将暂存区内容回退到工作区shuais-MacBook-Pro:restore shuai$ git restore --staged .# 5.查看文件状态,已经把添加的错误内容退回到了工作区shuais-MacBook-Pro:restore shuai$ git statusOn branch masterChanges 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.txtno changes added to commit (use "git add" and/or "git commit -a")# 6. 将工作区的内容撤销shuais-MacBook-Pro:restore shuai$ git restore .
演示从仓库中撤销内容
撤销本地仓库内容,
可以使用reset命令。
shuais-MacBook-Pro:restore shuai$ echo "this is wrong! " >> readme.txtshuais-MacBook-Pro:restore shuai$ git add .shuais-MacBook-Pro:restore shuai$ git commit -m "add wrong content"[master cda7c9b] add wrong content1 file changed, 1 insertion(+)shuais-MacBook-Pro:restore shuai$ git statusOn branch masternothing to commit, working tree cleanshuais-MacBook-Pro:restore shuai$ git log --onelinecda7c9b (HEAD -> master) add wrong contentd9531bd add file4cff528 v3bdaa5d3 v22688ed0 v1# 使用reset 默认参数【--mixed】,可以回退暂存区和本地仓库内容,这样还可以在工作区继续修改编辑# 也可使用--soft参数,只回退暂存区内容# 也可使用--hard参数,同时回退工作区,暂存区,本地仓库的内容。把修改的内容全部清空回退,无法再次编辑shuais-MacBook-Pro:restore shuai$ git reset HEAD^Unstaged changes after reset:M readme.txt
查看工作区、暂存区、本地仓库的差异
# 工作区和暂存区差异shuais-MacBook-Pro:restore shuai$ git diff readme.txtdiff --git a/readme.txt b/readme.txtindex 641d574..0d15209 100644--- a/readme.txt+++ b/readme.txt@@ -1,3 +1,4 @@111222333+this is wrong!# 暂存区和本地仓库无差异shuais-MacBook-Pro:restore shuai$ git diff --cached readme.txt# 工作区和本地仓库的差异shuais-MacBook-Pro:restore shuai$ git diff HEAD readme.txtdiff --git a/readme.txt b/readme.txtindex 641d574..0d15209 100644--- a/readme.txt+++ b/readme.txt@@ -1,3 +1,4 @@111222333+this is wrong!
说明使用git reset HEAD命令回退了暂存区和本地仓库的内容,将修改内容退回到工作区。
撤销远程仓库内容
操作远程仓库的撤销,最好使用revert回滚操作,这个操作可以新增一次commit提交记录,以免后边出现问题,能够进行查看修复。
使用reset回退操作,会把以前的提交commit记录给重置掉,且不会新增commit提交记录。该操作之后进行push操作无法正常进行,如果一定要同步远程仓库内容,可以使用git push -f强制操作。但是注意⚠️,该操作只能作用在自己的分支中,对公共分支进行此操作后,会造成其它人无法更新代码。
restore、reset、checkout对比
checkout 回退文件用法
git checkout -- <filename> 丢弃工作区的内容,并将最近一次commit内容回退到工作区。git checkout HEAD^ -- <filename> 将本地仓库中指定commit记录的内容还原到当前工作区。git checkout <branchname> -- <filename> 将指定分支的指定提交commit记录的内容还原到当前分支工作区
restore用法
git restore <filename>撤销 工作区的内容git restore --staged <filename> 撤销 暂存区的内容回退到工作区
reset 用法
git reset HEAD:回退暂存区和本地仓库的内容,将这2个部分的内容退回上一个commit的记录git reset --hard HEAD:回退本地仓库、暂存区、工作区的内容,将3个部分的内容都进行回退。
git checkout .命令和git restore .命令的效果一样,都是撤销工作区内容的修改。
