How to complete git version fallback in idea?

Time:2022-5-24

@[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:

  1. The code in the workspace wants to be undone
  2. The code of add to the temporary storage area wants to be revoked
  3. The code submitted to the local warehouse wants to be revoked
  4. 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:

How to complete git version fallback in idea?

p182

First, we implementedgit statusCommand, 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 statusCommand, the state of the workspace has changed, and then we execute itgit checkout -- git01.txtCommand, which means to undo the previous operationgit01.txtRestore to the previous state. After the command is executed successfully, we can execute it againcatThe 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:

  1. Undo the code of the staging area to the workspace
  2. 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 HEADCommand. As shown below:

How to complete git version fallback in idea?

p183

The code here is relatively simple, and the core process is to execute it firstgit reset HEADThe 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:

  1. have access toHEAD^To describe the version, a^Indicates the previous version, two^^Represents the first two versions, and so on.
  2. Numbers can also be used instead^For example, before100Two versions can be writtenHEAD~100
  3. 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:

  1. adoptgit logTo view the current submission log:
How to complete git version fallback in idea?

p184
  1. adoptgit reset HEAD^^Back two versions forward:
How to complete git version fallback in idea?

p185
  1. Check the log and find that the version number of the last submission is695ce1fe, usegit reset --hard 695ce1feThe command returns to the state before fallback:
How to complete git version fallback in idea?

p186
  1. adoptgit reset --hard HEAD~1Back to the previous version:
How to complete git version fallback in idea?

p187

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:

How to complete git version fallback in idea?

If you modify the file, whether it is executed or notgit addCommand. 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:

How to complete git version fallback in idea?

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:

How to complete git version fallback in idea?

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

How to complete git version fallback in idea?

The submission log is similar to the following:

How to complete git version fallback in idea?

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:

How to complete git version fallback in idea?

Right click other commit:

How to complete git version fallback in idea?

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:

How to complete git version fallback in idea?

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:

How to complete git version fallback in idea?

A submit dialog box will pop up, which is a common commit dialog box, as follows:

How to complete git version fallback in idea?

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:

How to complete git version fallback in idea?

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 ~

Recommended Today

Springboot Druid dynamic (multiple data sources)

Druid: Based on Apache 2 0 protocol Alibaba open source database connection poolDynamic: springboot based initiator for fast integration of multiple data sources What can Druid do Druid is more than just a database connection pool1. Monitor database access performance. A statfilter plug-in is built-in, which can make detailed statistics on SQL execution performance.2. Database […]