Git discards local changes

Divided into three situations:

  1. 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 checkoutcommands to undo the modification, such as:

 

git checkout -- rainbow.txt start.txt
git checkout -- *
git checkout -- *.txt


  1. The change has been added to the temporary storage area, that is, the git addcommand has been executed , such as:


    At this time, you can use git resetcommands 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.



  1. The code has been submitted to the local warehouse, that is, the git commitcommand 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 logor git reflogcommand to view submitted git history, get commit_id.

 

Guess you like

Origin blog.csdn.net/mango9126/article/details/105944942