How to complete Git version rollback in IDEA?


After last week's article was published, some friends asked how to roll back the Git version in IDEA?

In fact, this Songge has written an article before, but it has been a long time, so today I will revisit this topic with my friends, and by the way, I will also talk about how to perform Git version rollback in IDEA.

Undo operations in Git can be grouped into four categories:

  1. The code in the workspace wants to be undone
  2. The code added to the staging area wants to be undone
  3. The code submitted to the local repository wants to be undone
  4. The code in the remote repository wants to be revoked

1. Based on the command line

1.1 The code in the workspace wants to be undone

Maybe one day I am writing code, and I have written it for a long time and found that I made a mistake. If I want to restore the original state, a stupid way is to delete the code I just wrote line by line, but this method is too expensive. We can use the git checkout -- <file>command to undo code changes in the workspace. As shown below:

p182

First we executed the git statuscommand and found that the workspace was clean, then executed the cat command and found that the file has only two lines of content, then added a line to the file through the vi editor, saved and exited, and executed the git statuscommand after exiting. When the state of the workspace has changed, then we execute the git checkout -- git01.txtcommand to undo the previous operation and git01.txtrestore to the previous state. After the command is executed successfully, we execute the catcommand to find that the file content has been restored, and execute git statusit again at this time. Status is also restored.

1.2 The code that add to the temporary storage area wants to be undone

If you want to revoke, but the code has been submitted to the staging area, don't worry, you can also revoke, in two steps:

  1. Undo the code from the staging area to the workspace
  2. Undo the code in the workspace (the specific operation is the same as in Section 1.1)

To undo the code in the staging area, we can use the git reset HEADcommand to do so. As shown below:

p183

The code here is relatively simple. The core process is to execute the git reset HEADcommand and undo it from the temporary storage area. For the rest of the operations, refer to Section 1.1.

1.3 The code submitted to the local warehouse wants to be revoked

Similarly, the code submitted to the local repository can also be undone. We can use the git reset --hard <版本号>command to implement version rollback. There are several different ways to write the version number in this command:

  1. Versions can HEAD^be , one ^for the previous version, two ^^for the previous two versions, and so on.
  2. Numbers can also be used instead ^, for example the 100previous version can be written HEAD~100.
  3. You can also write the version number directly, indicating that you jump to a certain version. Each time we submit a successful submission, a hash code will be generated as the version number, so here we can also fill in the version number directly. The hash code is very long, but we don't need to enter all of them, just enter the first few characters, and then can be identified.

See the following series of operations:

  1. By git logviewing the current commit log:

p184

  1. git reset HEAD^^Go back two versions forward by :

p185

  1. Check the log and find that the version number of the last commit is 695ce1fe, use the git reset --hard 695ce1fecommand to return to the state before the rollback:

p186

  1. By git reset --hard HEAD~1going back to the previous version:

p187

Of course, the above operations are all based on the command line. If you are more skilled in command line operations, in fact, command line operations are much faster than IDEA.

1.4 Remote warehouse revocation

If the code is submitted to the remote warehouse and you want to revoke it, as mentioned in Section 1.3, you can undo it in the local warehouse first, and then push it to the remote warehouse.

2. Based on IDEA

After understanding the command line operation, it is much easier to see the IDEA-based operation.

2.1 Cancellation without submission

For the first two undo operations in the first subsection, that is, the modified file has not been committed, and if you want to undo it at this time, the method is very simple, click the undo button in the upper right corner of IDEA:

If you modify the file, no matter whether the git addcommand , as long as there is no commit, you can undo the modification through this button. Click this button, and the following prompt box will pop up:

All files that have been modified but have not been committed are listed here. If you want to undo the modification of which file, check the file, and then click the Rollback button to complete the undo operation.

2.2 Commit and want to undo

If you have already committed, then you need to open the commit log first, click the following button to open:

You can also directly click the clock icon in the upper right corner of IDEA to quickly open the commit log:

The commit log looks like this:

The fallback at this point is situational.

First, there are two types of undo operations:

  • Revert Commit
  • Undo Commit

Let's look at them separately.

2.2.1 Undo Commit

Undo Commit This operation can only be used on the most recent commit, not on other commits. On the most recent commit, right-click, as shown below:

Right click on other commits:

In this case, let's take a look at how the most recent commit Undo Commit.

Right-click on the last commit log and select Undo Commit, as shown below:

After selecting, click OK directly to revoke the latest commit.

This is to revoke the most recent commit. After the revocation, the local modification is equivalent to a state that has been added but not committed. At this time, we can continue to develop new code, then commit and push again; or as described in Section 2.1 That way, go ahead and undo the operation.

IDEA on my computer has a sporadic problem in this operation, that is, after I revoke the commit, IDEA cannot detect that the file is in an uncommitted state. I need to close IDEA and reopen it, and IDEA can find that the file is in an uncommitted state. At this point, you can continue to roll back according to the steps in subsection 2.1. This small partner can pay attention to it when testing.

2.2.2 Revert Commit

Revert Commit This operation can be used everywhere, different from Undo Commit, after Revert Commit, a commit record will be generated. The equivalent of Revert Commit is actually a submission, but the content of the submission is just the opposite, just brushing out the existing content.

The Revert Commit operation can be used on all logs, not just the commit just committed.

The operation method is as follows:

Find the place where you need to roll back, right-click, and select Revert Commit:

At this point, a submission dialog box will pop up, which is an ordinary commit dialog box, as follows:

After commit, you can see that the content has been revoked, and there is an additional record in the commit log, as shown below:

2.3 Push and want to undo

If it has been pushed to the remote warehouse, how to undo it?

In fact, as in Section 2.2, first undo in the local warehouse, re-modify the code after the undo is complete, and finally force push, but when force push, be careful not to overwrite the code of your colleagues.

3. Summary

Okay, today I shared with my friends a few revocation problems in Git. Those who have problems are welcome to leave a message for discussion~

Guess you like

Origin blog.csdn.net/u012702547/article/details/123198915