In daily development, we will inevitably create a wrong git submission record. Throughout this time, GIT provides us with two commands to solve this problem. One command is git reset, the other is git revert. What’s the difference between the two? The main difference between the two is that the GIT reset command is to roll back a certain submission, and the rolled back submission will not appear in the submission record, while the GIT revert command is to create a new submission to achieve the purpose of revocation, and both the revoked submission and the revoked submission will appear in the Submission record.
First of all, we still need to prepare a git warehouse and a file that can be modified. The file name doesn’t matter. Here I create a readme.md file:
mkdir git-test cd git-test git init touch README.md git add * git commit -m "init"
1. Modify the readme file.
2. Submit this modification.
Git commit "wrong commit"
3. Assuming that the just submitted one is wrong, we need to fallback. This is a git reset command that will be useful. We can use it to fallback to the specified submission.
$ git reset e431092f22a85deebf7bd6f4f96d9943530b49bf Unstaged changes after reset: M README.md
4. When we use git status to view the status, we will find that the readme file is in a modified state and the content has not changed. That is to say, GIT reset will know the pointer of the local warehouse to the branch you specify, but the content will not be discarded, but will be put into the workspace. If you want to submit again, it is OK.
$ 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: README.md no changes added to commit (use "git add" and/or "git commit -a")
If you want to discard it directly, you can add the — hard parameter. In fact, GIT reset can be rolled back in three ways:
Git reset -- hard < rollback Branch > Git reset -- soft < rollback Branch > Git reset -- mixed < rollback method >
- –Changes from the hard rollback branch to the current branch will be discarded completely.
- –Software will put the modified content in the temporary storage area, and you can still use the GIT commit command to submit.
- –Mixed is the default parameter of reset, that is, if you do not specify the parameter, you will use — mixed rollback by default, which will put the modified content into the workspace.
In some cases, we may not want the wrong submission record to be completely erased, or we want it to be saved in Git’s submission record. In case of an error in our revocation, we can have the opportunity to remedy it. In this case, we need to use the GIT revert command.
1. Let’s check the submission records of the current git warehouse.
$ git log commit 28aef5f811895f7124a3ae0e3b095a37ef9cb299 (HEAD -> master) Author: endless code < [email protected] > Date: Sun Jun 23 18:08:30 2019 +0800 Wrong submission commit e431092f22a85deebf7bd6f4f96d9943530b49bf Author: endless code < [email protected] > Date: Sun Jun 23 17:23:44 2019 +0800 init
2. Now we need to undo the wrong submission and execute the following command.
git revert 28aef5f811895f7124a3ae0e3b095a37ef9cb299
In the process of execution, a VI editing interface will pop up. Let’s edit the message of undo submission. The default is “undo submitted message”. Of course, we can edit it.
3. When you use the GIT log command to view the submission log, we will find that both the revoked submission and the revoked submission appear in the submission record. Then you can check the contents of the readme file and find that it has been rolled back.
$ git log commit ef836162c8f4cf75086151e517339789dd937453 (HEAD -> master) Author: endless code < [email protected] > Date: Sun Jun 23 18:13:27 2019 +0800 Revert "wrong commit" This reverts commit 28aef5f811895f7124a3ae0e3b095a37ef9cb299. commit 28aef5f811895f7124a3ae0e3b095a37ef9cb299 Author: endless code < [email protected] > Date: Sun Jun 23 18:08:30 2019 +0800 Wrong submission commit e431092f22a85deebf7bd6f4f96d9943530b49bf Author: endless code < [email protected] > Date: Sun Jun 23 17:23:44 2019 +0800 init
This article mainly talks about two ways of rollback error submission. There are quite a lot of normal use scenarios. I hope it can help you.
Well, that’s all the content of this article. I hope that the content of this article has a certain reference learning value for your study or work. Thank you for your support for developpaer.