git——撤銷某次提交:reflog & revert


1.查詢提交記錄。進入目錄,查看某人在此目錄下的commit:

panxi@ww-bj-panxi MINGW64 [path]/Business (feature/v2.3)
$ git log feature/v2.3 --author=[pan] -- ./

2.根據提交的commit,撤銷某次具體的commit(注意切換分支至目標分支):

git revert  94d26b9c189497c6cda72dffb0c9afee6cb8fb96

  

 

3.撤銷本地merge(原文鏈接:Undo a Git merge that hasn't been pushed yet

 

 

With git reflog check which commit is one prior the merge (git reflog will be a better option than git log). Then you can reset it using:

git reset --hard commit_sha

There's also another way:

git reset --hard HEAD~1

It will get you back 1 commit.

Be aware that any modified and uncommitted/unstashed files will be reset to their unmodified state. To keep them either stash changes away or see --merge option below.


As @Velmont suggested below in his answer, in this direct case using:

git reset --hard ORIG_HEAD

might yield better results, as it should preserve your changes. ORIG_HEAD will point to a commit directly before merge has occurred, so you don't have to hunt for it yourself.


A further tip is to use the --merge switch instead of --hard since it doesn't reset files unnecessarily:

git reset --merge ORIG_HEAD

--merge

Resets the index and updates the files in the working tree that are different between <commit> and HEAD, but keeps those which are different between the index and working tree (i.e. which have changes which have not been added).


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM