For more articles, visit my blog – code is endless.

Git provides two commands to solve this problem. One command is git reset and the other is Git Revert. What’s the difference? Git reset reverses a commit and does not appear in the commit record. Git Revert creates a new commit and does not appear in the commit record. Both the deleted and deleted commit appear in the commit record.

To prepare

First of all, we need to prepare a Git repository and prepare a file that can be modified. The file name doesn’t matter, so I created a readme. md file:

mkdir git-test
cd git-test
git init
touch README.md
git add *
git commit -m "init"
Copy the code

git reset

1. Modify the README file. 2. Commit the change.

git commit "Wrong submission"
Copy the code

3. If the commit was an error and we need to go back, this git reset command is useful. You can use it to go back to the commit.

$ git reset e431092f22a85deebf7bd6f4f96d9943530b49bf
Unstaged changes after reset:
M       README.md
Copy the code

4. Git status: The README file is in a modified state and its contents remain unchanged. Git reset will send the pointer to the local repository to the branch you specified, but the content will not be discarded. Instead, it will be put into the workspace, and you can submit it again if you want.

$ 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")
Copy the code

If you want to discard it, add –hard. Git reset has three rollback modes:

Git reset --hard < rollback branch > git reset --softCopy the code
  • --hardChanges made between the rollback branch and the current branch will be completely discarded.
  • --softThe changes will be put into the staging area and you can still use themgit commitCommand submission.
  • --mixedIs the default parameter of reset, which is used by default if you do not specify a parameter--mixedRollback, which puts the changes into the workspace.

git revert

In some cases, we may not want the wrong commit to be wiped completely, but rather want it to be stored in git’s commit record so that we have a chance to remedy the error if we undo it. In this case you need to use the Git Revert command.

1. Let’s look at the commit record of the current Git repository.

$ git logCommit 28 aef5f811895f7124a3ae0e3b095a37ef9cb299 (HEAD - > master) Author: code endless < [email protected] > the Date: Sun Jun 23 18:08:30 2019 + 0800 error submission commit e431092f22a85deebf7bd6f4f96d9943530b49bf Author: <[email protected]> Date: Sun Jun 23 17:23:44 2019 +0800 initCopy the code

2. Now we need to undo the wrong commit by executing the following command.

git revert 28aef5f811895f7124a3ae0e3b095a37ef9cb299
Copy the code

A VI edit screen pops up that allows you to edit the undo message (default: “Undo message”). You can also edit it. Git log: undo commit (s) : commit (s) : commit (s) : commit (s) : commit (s) : commit (s) : commit (s) : commit (s)

$ git logCommit ef836162c8f4cf75086151e517339789dd937453 (HEAD - > master) Author: code endless < [email protected] > the Date: Sun Jun 23 18:13:27 2019 +0800 Revert"Wrong submission"This reverts commit 28aef5f811895f7124a3ae0e3b095a37ef9cb299. commit 28aef5f811895f7124a3ae0e3b095a37ef9cb299 Author: Endless code <[email protected]> Date: Sun Jun 23 18:08:30 2019 + 0800 error submission commit e431092f22a85deebf7bd6f4f96d9943530b49bf Author: <[email protected]> Date: Sun Jun 23 17:23:44 2019 +0800 initCopy the code

conclusion

This article is mainly about the rollback error submission of the two ways, usually the use of a lot of scenarios, I hope to help you. Finally, if you like my article, you can scan the qr code below to follow the code endless public account, thank you for your support.