This is the 19th day of my participation in the Genwen Challenge
In github, you can sometimes fail to revert a submission. This article describes two ways to resolve the problem: reset and revert.
Git reset
Principle: The git reset command is used to roll back a version submitted once.
Specific operation:
The syntax format of git reset is as follows:
git reset [--soft | --mixed | --hard] [HEAD]
Copy the code
— Mixed is the default and can be used without this parameter. It is used to reset the file in the staging area to be the same as the last commit, while the workspace file content remains unchanged.
The soft parameter is used to rollback to a version. This is usually used for rollback after git add
git reset --soft HEAD
Copy the code
The –hard parameter is used to push back to a version, which is typically used after a Git commit but not yet pushed to the remote end
This fallback method does not preserve all of the code locally modified, so be careful when using it
git reset --hard HEAD
Copy the code
There’s another way I use it
Git reset [--hard] origin/masterCopy the code
This command will roll back the local code directly to the latest code in the main branch
Git Revert
Git revert is used to “reverse” a version to undo changes made to that version. For example, we commit three releases (version 1, version 2, and version 3) and suddenly discover that version 2 doesn’t work (e.g. If you want to undo version 2 without affecting a commit to undo version 3, you can use git revert to reverse version 2 and create a new version 4 that retains version 3 but undoes version 2. As shown below:
Application scenario: If we want to undo a previous version, but we want to keep the later version of the target version and record the entire version change process, we can use this method.
For example, there are three files in the library: read.md, text.txt, and text2.txt.
- Check the version number: you can check the version number through the command line (enter git log) : as shown in the figure, the latest two versions are respectively called “Add text. TXT” (text.txt) and “Add Text2.txt” (text2.txt). TXT file is not needed at this point, which means that we do not want to “add text. TXT” version of the operation, that can be done by “add text. TXT” version.
You can also view the version number on github’s graphical interface:
To do this, run the git revert -n version command: git commit -m The following command is used to reverse the version 8B89621:
Git revert -n 8 b89621019c9adc6fc4d242cd41daeb13aeb9861 1 note: there may be conflict, you will need to manually modify the conflict to file. And git add the filename. Git commit -m
Git commit -m “add text. TXT” git commit -m “Add text. TXT” But keep the “add text2.txt” version:
3. Use “Git push” to push remote library:
Git push 1 Git push 2 git push 1 git push
If you look at the repository file, there are two remaining: read.md and text2.txt