How does Git undo and rollback
I decided to familiarize myself with git version management tools. Git undo and rollback are often used in projects. Therefore, I made a summary and listed common undo commands.Copy the code
1 Undo the command before git commit
Below is the file I didn’t add to the staging area (i.e., not executed)git add
This command)This undo can be performed directlyGit checkout -- file name
Take a look at the code example (be careful -) that just had two files executedGit checkout -- file name
After the command, there is only one file left, and the other file is destroyed.Undo multiple filesgit checkout -- .
Can undo multiple files
2. Add to the staging areagit add
How do I cancel it? Take a look at the picture belowgit add .
After that, Changes to be committed have been added to the staging areaNow I’m going to undo the file I added to the staging area using the commandgit reset HEAD <file>
File is the name of the fileI checked the status againgit status
It shows that I have not yet added to the staging area. If I want to undo all the files added to the staging area, I can do sogit reset HEAD
Command.
The second most common situation is after git commit. At this point it has been committed, just not pushed to the remote repository
To undo a COMMIT, you need to revert. The Git Revert command undoes an operation and keeps all commits before and after the operation.
I use firstgit log
Check the submission record. Forget it. I hategit log
Print too much, use this command directlygit log --pretty=oneline
The most recent record I committed appears directly below, and the big yellow string in front is the commit ID (version number) I committed.Now I’ll roll it back to the command I just executed before committingGit Revert version number
Then update the remote repository code after pushing it to the remote end, and the modified file is undone back. Note that the revert revert revert takes effect on an odd number of entries and reverts back to its previous state. For example, if a file is a and you change it to AB, you revert it to A and then you revert it back to AB.
I can still use itgit reset --hard HEAD^
So this is going to go back to the last version, go back to the last two versions and so on and so on and so on and so on and so on and so on and so on and so on and so on and so on and so on and so on and so on and so onHEAD~100
I submitted it again just now, and now it is “Demo Test 2”.Now I’ll roll it back two versions later- hard -
Forces both the cache and the working directory to be synchronized to the commit you specify Note that it is possible to fall back to a commit and all subsequent commits will be rolled back, but this override is irreversible. [add links to describe] (https://www.liaoxuefeng.com/wiki/896043488029600/897013573512192)), before submit records all have no. Therefore, in normal development, try to pay attention to avoid using reset.
Next and then commit to the remote overwrite