Git operation manual – how to achieve accurate rollback

Time:2021-10-13

catalogue

  • 1、 How to roll back files modified in the workspace (GIT checkout)

    • Restore a single file
    • Recover all files
    • Compiler: vsccode source control
  • 2、 How to roll back files modified in the staging area (GIT reset)

    • Restore the specified files from the staging area to the workspace
    • Restore all files in the staging area to the workspace
    • Compiler: vsccode source control
  • 3、 How to roll back a submitted file (GIT reset, GIT revert)

    • Roll back the newly committed version to the uncommitted state
    • Rollback to the previously specified version
  • 4、 How do remote machines roll back

    • Method 1: first roll back git reset to the local, and then force push to the remote.
    • Method 2: first, GIT revert will correct the problematic version, generate a new version, and then psuh to the remote branch.
  • git reset VS git revert

1、 How to roll back files modified in the workspace (GIT checkout)

Restore a single file

git checkout -- README.md

Recover all files

git checkout .

Compiler: vsccode source control

Git operation manual - how to achieve accurate rollback

2、 How to roll back files modified in the staging area (GIT reset)

Restore the specified files from the staging area to the workspace

git reset README.md

Restore all files in the staging area to the workspace

git reset

Compiler: vsccode source control

Git operation manual - how to achieve accurate rollback

3、 How to roll back a submitted file (GIT reset, GIT revert)

Roll back the newly committed version to the uncommitted state

git reset HEAD^
git reset @^

Tips: this command rolls back the submitted file to the workspace state. If it needs to be modified againgit add .

Rollback to the previously specified version

git reset commitID
git revert -n commitID

4、 How do remote machines roll back

Method 1: first roll back git reset to the local, and then force push to the remote.

Not recommended, may not have permission, dangerous operation

git reset commitID
git push -u origin master -f

Method 2: first, GIT revert will correct the problematic version, generate a new version, and then psuh to the remote branch.

Equivalent to patching, recommended

git revert -n commitID
#Handle conflicts manually
     #If conflict handling completed
     git revert --continue
     #If exit does not handle conflicts
     git revert --abort
#Enter the VIM interface, write the commit description and save it
git push origin master

git reset VS git revert

Used in rollback abovegit resetandgit revertTwo methods are analyzed as follows:

compare Same point difference
git reset Rollback operation can be performed git resetAfter rollbackcommitIDLater versions are not recorded< Br / > if you want to rollback to a future node, you need to know the status of the future nodecommitID。< Br / > so you’d better know the current status before rolling backcommitID
git revert Rollback operation can be performed git revertAfter the rollback operation, the files will be removed or retained manually, < br / > and a new version number will be generated. The previous submission is still in progress.