Idea undoes git add or commit (not pushed) or pushed files

The difference between Undo Commit, Revert Commit and Drop Commit

Whether to delete modifications to the code Whether to delete the Commit record Will a new Commit record be added?
Undo Commit Won't It will work if it has not been pushed, but it will not work if it has been pushed. Won't
Revert Commit meeting Won't meeting
Drop Commit meeting It will work if it has not been pushed, but it will not work if it has been pushed. Won't

I. Introduction

After executing a commit in an idea (without pushing), I found that the submitted code was wrong and wanted to withdraw it. This article summarizes how to withdraw in this case.

As shown below: (commit submitted a test record (not pushed), and added the Test.java file)
Insert image description here

2. Return the commit (not pushed)

2.1. Method 1: undo commit

Applicable situation: The code has been modified and committed, but has not been pushed yet. Then I find that there are still places that need to be modified and I don’t want to submit this record. At this time, you can Undo Commit and then commit again after modification.

If Push has been performed, the online Commit record will still exist.

To put it simply, it is to cancel your Commit action. Explain in detail:

1. First, the code of the project is modified, and then the commit operation is performed.
Insert image description here

2. After confirming the Commit (no push is performed)
Insert image description here
3. Perform the Undo Commit operation
. Right-click the record to be revoked and select undo commit.
Insert image description here

Insert image description here

After execution, it is exactly the same as before committing, and the commit record is gone.
Insert image description here

2.2. Method 2: reset current branch to here

Applicable situation: committed but not pushed

Right-click the last record of the error record, and then select reset current branch to here.
Insert image description here
Insert image description here
After selecting Mixed to cancel, the commit record will be deleted, and the submitted files will be entered into the Unversioned Files directory.
Insert image description here

  • Soft: Roll back to the specified version and keep all changes, which will be resubmitted. Using this mode, you can undo changes that have been committed and then resubmit them.
  • Mixed: Roll back to the specified version and keep all changes, but the changes will not be resubmitted (the files go into Unversioned Files). Using this mode, you can undo changes that have already been committed and retain them for subsequent modifications and commits.
  • Hard: Roll back to the specified version and delete all changes after the specified version. Using this mode, you can completely undo all changes after a specified version.
  • Keep: Roll back to the specified version, but keep changes after the specified version. Using this mode, you can undo changes made after a specified version and retain those changes for subsequent revisions and commits.

keep和hard撤销后会把文件也删除掉,强烈建议不要使用或者慎用

3. Return push

3.1. Method 1: Reset HEAD

Note: This return will return to the version number you specified regardless of whether you committed without pushing or the commit has been pushed.

Step 1. Idea finds the project commit record list:
Right-click on the project name-Git-Show History or Version Control under the idea can open the record information of historical commits.
Insert image description here
Step 2. Select the version number that needs to be rolled back to:
For example, my latest code is Test2 needs to roll back to the version submitted by Test1 last time

Select the record corresponding to the submission and right-click - Copy Revision Number; Copy the commit number.
Insert image description here
Step 3. Roll back to the corresponding version
. Right-click the project name - Git - Repository - Reset HEAD.
Insert image description here
Step 4. Enter the commit number that needs to be rolled back.
Select the Reset Type in the pop-up box in the first step - the commit number copied in the second step - Reset

This Reset Type has three options, choose the optional parameters according to your own wishes:

First understand:
workspace - staging area - local warehouse
Code writing and modification are in the workspace - git add adds local modifications to the staging area - git commit submits the contents of the staging area to the local warehouse

  • mixed
    does not delete the workspace, change the code, 撤销commitand 撤销git add . operate, return to the workspace
  • Soft
    does not delete the workspace to change the code, 撤销commitand 不撤销git add . the operation is
    rolled back to before git commit, which is in the temporary storage area at this time. (That is, after executing the git add command)
  • hard
    撤销commit,撤销add,删除本地改动代码。
    (三者的改变全都丢失,即代码的修改内容丢失,直接回退到某个版本;因此我们修改过的代码就没了,需要谨慎使用)

I want to roll back to the record submitted by Test1, and all the code after Test1 is completely unnecessary, so I chose Hard
Insert image description here
Step 5. Push the rolled back code
and select Terminal under IDEA - enter git push -f on the command line; force push Go to the remote warehouse
Insert image description here
step 6. Completed
. It can be seen that there is no record of Test2 submission in the code submission record, and it has been rolled back to the Test1 version.
Insert image description here
As you can see, the submission just now has been revoked, but other code content that I modified locally has also been overwritten and is gone. So be sure to use it with caution. If you have unsubmitted code for other functions in the workspace at this time, just will be deleted

Guess you like

Origin blog.csdn.net/weixin_49114503/article/details/131767667