[Git] The difference between git reset and git revert

git reset

  • Realization: The function of git reset is to modify the position of HEAD
  • git reset [–soft | --mixed | --hard

--mixed
The source code will be retained, but the git commit and index information will be rolled back to a certain version.
Git reset defaults to --mixed mode (git reset --mixed === git reset) to
--soft
retain the source code, and only roll back to commit information to a certain version Version. No index rollback is involved. If you still need to submit, just commit directly. The
--hard
source code will also fall back to a certain version, and both commit and index will fall back to a certain version. (Note that this method is to change the local code Warehouse source code)

  • Implementation process:
    Insert picture description here

  • Applicable scenario: If you want to revert to a previously submitted version, and we don't want any of the submitted versions after that version, you can use this method.

  • Operation:
    1. After commit is the version number
    git log
    Insert picture description here

    2. Modify the target version
    git reset --hard'target version number'

    3. Check the version information, at this time the local HEAD has pointed to the previous version
    git log

    4. Submit (git push will report an error because the submitted version is lower than the server version)
    git push -f

    5. If you don't want to resolve the conflict, you can cancel the withdrawal
    git revert --abort

git revert

  • Principle: To revoke the submission of one version without affecting other versions. For example, we committed three versions (version 1, version 2, and version 3), and suddenly found that version 2 was not working (for example, there was a bug), and wanted to revoke version 2, but did not want to affect the submission of revoked version 3. The git revert command reverses version two and generates a new version four. In this version four, the things in version three are retained, but the things in version two are undone.

  • Implementation process:
    Insert picture description here

  • Operation:
    1. View the submitted version
    git log

    2. Specify the modified version
    git revert -n'version number'

    3. Submit the modification
    git add. → git commit -m version name

    4. Push server
    git push

Guess you like

Origin blog.csdn.net/m0_46537958/article/details/108326551