Git misoperation, completely roll back the version to the previous version

  1. Find the version commit that needs to be rolled back

    The git log command line lists all commits and finds the commit that needs to be rolled back

    or

    Idea right-click the project git -> show history, find the commit that needs to be rolled back in the list, right-click Copy Revision Number, and copy the commit

  2. Go back after finding out the commit

    git reset --hard e7d585bfecaffc885f252cea13b9801f41f075ad(这是commit,需要改成自己的)

  3. Rollback is successful, push

    git pushYou will be rejected and you can force push git push -f. Forced push is very dangerous. Make sure that the entire team agrees to force a restoration to this version before doing this operation.

  4. Note: If someone has already pulled the code you mis- 2operated , please force the push and let him perform the operation locally . After this reset, perform the pull operation again to ensure that you will not pull to the version you rolled back, but He still has the previous code locally and resubmit it again.

Expand knowledge:

git reset是指将当前head的内容重置,不会留log信息。

    git reset HEAD filename  从暂存区中移除文件
    git reset –hard HEAD~3  会将最新的3次提交全部重置,就像没有提交过一样。
    git reset –hard commit (38679ed709fd0a3767b79b93d0fba5bb8dd235f8) 回退到 38679ed709fd0a3767b79b93d0fba5bb8dd235f8 版本
    
    
根据–soft –mixed –hard,会对working tree和index和HEAD进行重置:

    git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息
    git reset –soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可
    git reset –hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容

Guess you like

Origin blog.csdn.net/cainiao1412/article/details/110122067