首頁  >  文章  >  開發工具  >  git中reset和revert的差別是什麼

git中reset和revert的差別是什麼

青灯夜游
青灯夜游原創
2021-11-30 14:57:2525709瀏覽

區別:1、reset是徹底回退到指定的commit版本,該commit後的所有commit都將被清除;而revert僅是撤銷指定commit的修改,並不影響後續的commit。 2、reset執行後不會產生記錄,revert執行後會產生記錄。

git中reset和revert的差別是什麼

本教學操作環境:Windows7系統、Git2.30.0版、Dell G3電腦。

git是我們常用的版本管理工具,我們團隊在合作開發專案時,時常會因為程式碼及文件的修改提交,導致各種各樣的衝突,還有產品需求的頻繁變更,致使我們不得不做出回退版本,撤回提交這樣的決定,那麼此時,reset和revert命令,就派上了用場!

reset,revert都有撤銷、回退的意思,但卻各有千秋,區別還是很大的,所以該使用哪種命令一定要結合實際情況來決定,本文就是帶大家搞清楚兩者的差別,然後能準確快速的使用正確的指令去解決實際問題!

下面的例子中,我有3次提交:
初始状态,只有readme一个文件,内容为Creating a new branch is quick.
t1提交后状态:只有readme一个文件,内容修改为Creating a new branch is quick 1.
t2提交后状态:只有readme一个文件,内容修改为Creating a new branch is quick 1 2.
t3提交后状态:新增了test文件.

本文以git bash為例:

git中reset和revert的差別是什麼
#先說reset

reset,使用方法:git reset --hard commit ,commit是提交後產生的SHA1,執行該指令後,程式碼會完全回退到本次提交時的狀態,工作暫存區以及本次提交後面的提交內容將會被完全清除,包括提交記錄!

範例:

原始項目,包含一個Readme.txt檔案:
git中reset和revert的差別是什麼
檔案內容:
git中reset和revert的差別是什麼
此時我將修改檔案內容為:

Creating a new branch is quick 1.

##進行

第一次提交
git中reset和revert的差別是什麼 提交記錄:

git中reset和revert的差別是什麼 提交後的遠端倉庫目錄及檔案內容:

git中reset和revert的差別是什麼git中reset和revert的差別是什麼沒有問題,繼續修改文件內容:
Creating a new branch is quick 1 2. ,進行第二次提交
git中reset和revert的差別是什麼 # 現在我將新增一個test文件,進行
第三次提交
git中reset和revert的差別是什麼
git中reset和revert的差別是什麼git中reset和revert的差別是什麼

好了,現在產品需求變更了,新增的功能(readme的第二次修改和新增的test檔)不需要了,要求回退到第一次提交“t1”,如果我們選擇使用reset:

首先定位到t1的commit,可以從遠端倉庫提交歷史記錄中複製,也可以用命令

git log查看:

git中reset和revert的差別是什麼
(小提示,如果最後一行出現“:”,則輸入wq退出回到命令列即可!)

複製commit,執行指令:

git reset --hard 8cbf16c0821d20fe42c361f4e3d75a0493dc5fc2

git中reset和revert的差別是什麼##提示,HEAD已經指向了t1,但當你刷新後台時,發現並沒有什麼變化,這是因為我們還需要執行一下push,但這裡需要注意的是,因為本地代碼回到了舊版本,但遠端倉庫是新版本和本地不一致,所以你在用git push時會報錯,這裡我們需要使用強制提交,

git push -f

,我們也可以使用git status查看當前狀態:

#

意思是告诉你,远程仓库代码较新,需要你执行 git pull操作以同步代码,但这并不是我们的需求,所以我们不用理会,执行,git push -f

![git中reset和revert的差別是什麼](https://img-blog.csdnimg.cn/94c3e93f6efe40b6a4d12d02eb05cd4d.png

再看仓库:

git中reset和revert的差別是什麼

历史记录只剩下了t1:

git中reset和revert的差別是什麼

readme内容也得到了恢复:

git中reset和revert的差別是什麼

可见,reset是彻彻底底的回退,该commit之后的所有修改将完全消失,包括提交记录。

优点

  • 彻底回退到指定版本,干净清爽;
  • 提交时间线清晰,没有冗杂;

缺点

  • 记录彻底清除,无法再次恢复;

再说revert

revert执行后会产生新的commit记录,是通过一次新的commit来恢复到之前旧的commit,但revert会保留恢复的该次提交后面的其它提交内容,假如后面的提交与要恢复的提交更改了同一地方,此时用revert就会产生冲突!

我们继续以上面的例子为例,我重新执行了t2和t3提交,恢复到reset之前的状态:

git中reset和revert的差別是什麼
git中reset和revert的差別是什麼
此时,我们按reset的思路,使用revert恢复到t1,执行命令:

git revert 8cbf16c0821d20fe42c361f4e3d75a0493dc5fc2

报错:

git中reset和revert的差別是什麼
提示冲突了?让我们解决掉冲突后提交…

<<<<<<< HEAD
Creating a new branch is quick 1 2.
=======
Creating a new branch is quick.
>>>>>>> parent of 8cbf16c (t1)

上面的冲突表示,当前的内容是:

Creating a new branch is quick 1 2.

而我们要恢复的内容是:

Creating a new branch is quick.

如果对revert命令没有深入了解的话,就可能会产生疑惑,为什么会冲突?而且我实际上是想像reset一样恢复或者说是回退到t1(这里要再次说明一下t1的状态:只有一个readme文件,且内容是Creating a new branch is quick 1),但为什么冲突提示要恢复到Creating a new branch is quick.???这不是初始状态吗?

其实,准确来说,revert是撤销/撤回/反提交的意思,我们不能按reset的思路理解,我们执行git revert t1,这么做其实结果是要撤销t1的提交,注意,仅仅是撤销t1的提交,把t1的修改恢复到t1之前也就是初始的状态,而不会影响t2,t3的提交。但如果t2,t3中修改了t1修改的同一地方,那么就会产生冲突,因为revert意图撤销t1的修改,但发现t2和t3把t1的修改再次修改了,此时,revert意图变得不清晰,因为它无法确定到底是应用你最新的修改,还是恢复到初始状态,这将由你来决定!

所以我们想要恢复t1的状态,那我们就应该撤销t2对t1的修改git revert t2

git revert fc4889dcb327cff9f8078db6a0d5c601b8e91ae9

执行后会自动进入编辑界面:

git中reset和revert的差別是什麼

这里需要我们修改或输入提交日志,按 “i”,进入输入状态,写完后按ESC退出输入状态,再按“:wq”退出!

成功后,执行 git push:

git中reset和revert的差別是什麼

查看仓库后台:

git中reset和revert的差別是什麼

项目目录:

git中reset和revert的差別是什麼

readme内容:

git中reset和revert的差別是什麼

可见,revert操作成功后,产生了新的commit记录,t2对t1的修改已经恢复,现在的readme就是t1提交后的状态,但同时test文件仍然存在,即t3的提交不受影响!

But if you want to delete the submission of t2t3 just like reset, then you can revert t3 first, and then revert t2, which can achieve the same effect. But in this case, why not use reset directly? ? If you want to achieve the effect of reset and also want to have a record to prevent regrets, then this is it. . . It’s a question worth thinking about!

Summary of the difference between git reset and revert:

  • reset is a complete rollback to the specified commit version. After the commit All commits will be cleared, including commit history;
  • revert only undoes the modification of the specified commit and does not affect subsequent commits. However, if the revoked commit is modified in the same place by a subsequent commit, an error will occur. Conflict;
  • No records will be generated after reset is executed, but records will be generated after revert is executed;
  • Cannot be restored after reset is executed, because records will not be cleared after revert is executed, and new records will be generated , so the file will not be lost. You can execute revert multiple times to restore to the state before a certain change;
  • HEAD will move backward after reset is executed, while revert's HEAD will always move forward;

After clarifying the basic principles of reset and revert, you will understand which command is more appropriate to use at what time!

Tips: In the IDEA development tool, select a file, right-click on the git option and you will find a Rollback

git中reset和revert的差別是什麼
This needs to be distinguished from reset and revert. Rollback does not belong to the git command. Its function is to restore after the file or code has been modified but has not yet been committed. When the state is consistent with the remote warehouse code, you can perform the rollback operation!

Recommended study: "Git Tutorial"

以上是git中reset和revert的差別是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn