Divided into three situations:
- The changes have not been added from the work area to the temporary storage area, that is
git add
, before the command is executed , such as:
At this time, you can use git checkout
commands to undo the modification, such as:
git checkout -- rainbow.txt start.txt
git checkout -- *
git checkout -- *.txt
- The change has been added to the temporary storage area, that is, the
git add
command has been executed , such as:
At this time, you can usegit reset
commands to undo the modification, such as:
git reset HEAD rainbow.txt start.txt
git reset HEAD *
git reset HEAD *.txt
It should be noted that after executing the above command, the local modification will not disappear, but only return to the working area from the temporary storage area , which is the state shown in the first case. Continue to use the operation in the first case, you can abandon the local modification.
- The code has been submitted to the local warehouse, that is, the
git commit
command has been executed . At this time, the work area is clean. If you want to undo the previous modification, you need to perform the version rollback operation:
#回退到上一个版本
git reset --hard HEAD^
#回退到上上次版本
git reset --hard HEAD^^
git reset --hard HEAD^^^
#回退到指定commitid的版本
git reset --hard commit_id
You can use git log
or git reflog
command to view submitted git history, get commit_id.