Temporary area restored to HEAD

When we need to discard changes in the staging area, we can use the reset command to roll back

Git add -u > git status > git status On branch master Changes to be committed: (use "git reset HEAD <file>..." Passage ten: < passage ten > < passage ten > < passage ten > < passage ten > < passage ten > Git status > git status On branch master Changes not staged for commit: (use "git add <file>...") to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: demo.html no changes added to commit (use "git add" and/or "git commit -a")Copy the code

Git reset HEAD

git reset HEAD

Other usage scenarios of the reset command

Git reset has three parameters. Soft restores the HEAD to the commit specified by you. Hard restores the HEAD to the staging area. Change the workspace to the file state you specified at commit time --mixed is the default parameter without adding time. Change the HEAD, temporary area to the file state you specified at commit time, and leave the workspace unchangedCopy the code

Restore workspace to staging area

When we need to restore the contents of the workspace to the staging area, we use the checkout command

> < div style = "padding-top: 0px; padding-top: 0px; padding-top: 0px; to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: demo.html no changes added to commit (use "git add" and/or "git commit -a")Copy the code

You can see that there is a change in the current workspace that has not been committed to the staging area. When you want to abandon it, you can do the following

> git checkout -- demo.html

> git status
On branch master
nothing to commit, working tree clean
Copy the code

After the operation, you can see that the workspace changes have been restored to a consistent state for the staging area

Restore HEAD to the state of a previous COMMIT

When we want to discard several commits on the current branch, we can use the –hard command of the reset command

Start by looking at the current COMMIT record

commit 83bc3c142f7dfda3dd2cae387b69bd7c7162d9b0
Author: your name <[email protected]>
Date:   Sun Dec 20 00:44:59 2020 +0800

    test commit

commit 20bbb14ede2297040f0d08fdde9712d80aea6984
Author: your name <[email protected]>
Date:   Sun Dec 13 19:02:57 2020 +0800

    commbo commit

    add demo.html and modify readme has been changed again!

    modify demo.html

commit 84ed89a79f290cbdab68bdc9468859a05d9fac77
Author: your name <[email protected]>
Date:   Sun Dec 13 18:57:21 2020 +0800

    init proj
Copy the code

When we want to roll back the commit to the middle commit (that is, abandon the last commit), we can do the following

> git reset --hard 20bbb14ede HEAD back to the us to specify the location of the commit 20 bbb14ede2297040f0d08fdde9712d80aea6984 Author: your name < your.com > Date: Sun Dec 13 19:02:57 2020 +0800 commbo commit add demo.html and modify readme has been changed again! modify demo.html commit 84ed89a79f290cbdab68bdc9468859a05d9fac77 Author: your name <[email protected]> Date: Sun Dec 13 18:57:21 2020 +0800 init projCopy the code

Reset-hard is a dangerous behavior. Do not use it unless you explicitly want to discard some changes. Lost code cannot be recovered!