@[toc]
After last week’s article was published, a friend asked how to roll back the GIT version in idea?
In fact, brother song has written articles before, but it’s a long time ago, so today I’ll touch the topic again with my friends and talk about how to roll back the version of git in idea.
Undo operations in Git can be classified into four categories:
- The code in the workspace wants to be undone
- The code of add to the temporary storage area wants to be revoked
- The code submitted to the local warehouse wants to be revoked
- The code of the remote warehouse wants to be revoked
1. Command line based
Code to undo workspace 1.1
Maybe one day I’m writing code. After writing for a long time, I find that I made a mistake and want to restore to the original state. A stupid way is to delete the code just written line by line, but this way is too expensive. We can pass itgit checkout -- <file>
Command to undo code changes in the workspace. As shown below:

First, we implementedgit status
Command, find that the workspace is clean, and then execute the cat command. It is found that there are only two lines in the file. Then add a line to the file through the VI editor, save and exit. After exiting, execute it againgit status
Command, the state of the workspace has changed, and then we execute itgit checkout -- git01.txt
Command, which means to undo the previous operationgit01.txt
Restore to the previous state. After the command is executed successfully, we can execute it againcat
The command finds that the contents of the file have been recovered. Execute it again at this timegit status
, the state is restored.
1.2 the code of adding to the temporary storage area wants to be revoked
If you want to undo, but the code has been submitted to the temporary storage area, don’t worry. You can undo it in two steps:
- Undo the code of the staging area to the workspace
- Undo the code in the work area (the specific operation is consistent with section 1.1)
Undo the code of the staging area, we can usegit reset HEAD
Command. As shown below:

The code here is relatively simple, and the core process is to execute it firstgit reset HEAD
The command is revoked from the temporary storage area. Refer to section 1.1 for the remaining operations.
1.3 the code submitted to the local warehouse wants to be revoked
Similarly, the code submitted to the local warehouse can also be revoked, which we can useGit reset -- hard < version number >
Command to realize version fallback. The version number in this command can be written in several different ways:
- have access to
HEAD^
To describe the version, a^
Indicates the previous version, two^^
Represents the first two versions, and so on. - Numbers can also be used instead
^
For example, before100
Two versions can be writtenHEAD~100
。 - You can also write the version number directly to jump to a version. After each successful submission, we will generate a hash code as the version number, so here we can also directly fill in the version number. The hash code is very long, but we don’t need to input all of it. We only need to input the first few characters to recognize it.
Here are a series of operations:
- adopt
git log
To view the current submission log:

- adopt
git reset HEAD^^
Back two versions forward:

- Check the log and find that the version number of the last submission is
695ce1fe
, usegit reset --hard 695ce1fe
The command returns to the state before fallback:

- adopt
git reset --hard HEAD~1
Back to the previous version:

Of course, the above operations are based on the command line. If you are proficient in command line operation, the command line operation is much faster than that on idea.
1.4 remote warehouse cancellation
If the code is submitted to the remote warehouse and you want to revoke it, as mentioned in Section 1.3, revoke 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 read the operation based on idea.
2.1 cancellation without submission
For the first two undo operations in the first section, i.e. the modified file has not been committed, the way to undo it is very simple. Click the undo button in the upper right corner of idea:

If you modify the file, whether it is executed or notgit add
Command. You can undo the modification by clicking this button as long as there is no commit. Click this button to pop up the following prompt box:

All modified files without commit will be listed here. If you want to undo the modification of any file, check the file, and then click the rollback button to complete the undo operation.
2.2 commit wants to revoke
If you have committed, you need to open the submission log first and click the following button to open it:

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

The submission log is similar to the following:

At this time, the fallback is divided into different situations.
First, there are two kinds of undo operations:
- Revert Commit
- Undo Commit
Let’s look at it separately.
2.2.1 Undo Commit
Undo commit can only be used on the latest commit, not on other commits. Right click on the latest commit, as shown in the following figure:

Right click other commit:

In that case, let’s take a look at how to undo commit in the latest commit.
Right click the latest commit log and select undo commit, as shown in the following figure:

After selecting it, directly click OK to cancel the latest commit.
This is to cancel the latest commit. After canceling, the local modification becomes the state of added but not committed. At this time, we can continue to develop new code, then commit and push; Or you can continue the undo operation as described in Section 2.1.
There is an accidental problem with the idea on my computer in this operation, that is, after canceling the commit, the idea cannot detect that the file is in the uncommitted state. If I need to turn off and reopen the idea, the idea can find that the file is in the uncommitted state. At this time, we can continue to retreat according to the steps in Section 2.1. This little partner can pay attention to it during the experiment.
2.2.2 Revert Commit
The operation of revert commit can be used everywhere. Unlike undo commit, a submission record will be generated after revert commit. It’s equivalent to reverse commit. In fact, it is also a submission, but the submitted content 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 submitted.
The operation mode is as follows:
Find the place to rollback, right-click and select revert commit:

A submit dialog box will pop up, which is a common 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 in the following figure:

2.3 push wants to cancel
If you have pushed to the remote warehouse, how to cancel it?
In fact, as in Section 2.2, first revoke the code in the local warehouse, modify the code again after revocation, and finally force push. However, when force push, be careful not to overwrite the code of colleagues.
3. Summary
Well, today I shared some cancellation problems in Git with my friends. If you have problems, please leave a message for discussion ~