preface

In the development process, there will often be code submission, want to undo the operation, so how to carry out a variety of Git undo operations? This article will share various methods of submitting the correct medicine to Git by mistake. Readers can choose to learn from the situation, or save this article for quick reference.

A picture is worth a thousand words

tip

Git editor is recommended to be opened by vscode or vim by default. The configuration method is as follows:

# configure vscode
git config --global core.editor "code --wait"

# configure vim
git config --global core.editor "vim"

Copy the code

What you need to know in advance

  • Git workspace, staging area, version repository
  • Git rebase, revert, reset

Break down each case by whether it has been committed or not:

1. Uncommitted undo methods

There are two scenarios for non-commit

1.1 No Commit, No Add

Scenario: A file has been added or changed locally, but a Git operation has not been done (git add.) You want to clean up local changes to be consistent with your Git repository.

This is the easiest case

STEP 1

Execute git checkout. To reset the modified files (excluding the new files, which have not yet been logged to Git version control).

STEP 2 (Optional)

To delete newly added files, you can either delete them one by one or run the git clean-df command to delete all files that are not under Git version control.

What is Git clean?

Git clean Clears files or directories that are not controlled by the Git version in the repository. -d indicates that the directory is deleted together. -f Forcibly deletes the directory. If clean.requireForce is true by default, add -f to remove it. In addition, use -f to delete directories with.git subdirectories or files

1.2 Add is not committed

Scenario: A file has been added or changed locally, but git add. has been executed. You want to clear the change and keep it consistent with the Git repository.

STEP 1

Execute Git restore –staged. Drag the file from staging back to the commit list

SETP 2

Follow the previous “No commit, no add” operation


What can I do if I commit? There are various remedies for different situations, but reset, Rebase, and Revert are some of them. You can check them out first.

2. Commit the undo method

2.1 Withdraw the last submission for re-editing

Scenario: A file has been added or changed locally, but a COMMIT has been performed and you want to revoke the last commit and edit it again.

STEP 1

First, make sure that the current workspace is clean after making an error commit. Git reset –soft HEAD^ to withdraw the commit. Note that soft soft reset is used to keep all local changes.

After executing git, you are back to the state after git add. whether you need to continue to modify or undo the added changes (refer to the above method), you can make it according to the actual situation.

STEP 2 (perform if pushed)

Git push -f origin git push -f origin git push -f origin This of course involves whether the branch protection policies of the warehouse restrict you from pushing the branch, and whether only you can make changes to the branch. For example, after you push, there are other development students push, so maybe you push the warehouse, the development brother will push you.

2.2 Discarding the latest submission

Scenario: Changes have been committed and you want to discard the last change without leaving a commit record

STEP

Git reset –hard HEAD^ resets the code base hard reset to the last commit. If you need to specify a commit ID, reset –hard a commitid

2.3 Rolling back a Commit

Scenario: The change has been committed and you want to discard the change, leaving a commit record

STEP

Execute git Revert to add a rollback commit.

2.4 Modifying the Commit Message

Scenario: After the commit, the commit Message is incorrectly filled in, and the commit Message needs to be modified.

STEP

Execute git commit –amend

Note: It can also be used to modify the last submission

2.5 Merge multiple submissions

Scenario: In the process of modifying a file A, several consecutive commit is made, hoping to merge multiple consecutive commit into one, other developers only see one commit.

For example, merge three commits into one

STEP 1

git rebase -i HEAD~3
Copy the code

STEP 2

The last three commits are displayed as follows

pick 690cb73 1
pick 6f6a377 2
pick 0a267b8 3

Copy the code

We change the last two commits to a Pick squash, and then save and exit the editor as follows

pick 690cb73 1
squash 6f6a377 2
squash 0a267b8 3
Copy the code

STEP 3

If there is a conflict, handle it and commit it again. Then the pop-up editor will input a new COMMIT message, save it and exit the editor, prompting you to complete the operation.

Successfully rebased and updated refs/heads/develop.

2.6 Overwriting a Commit Record

This section is excerpted from the rebase-I modification commit

Scenario: commit A, B, and C successively, but have not pushed, want to entrap private goods, and then make more changes to the historical B commit.

STEP 1

git rebase -i HEAD~~
Copy the code

STEP 2

Change the “pick” on the first line to “Edit”, then save and exit.

pick 09ba200 b
pick bc376e3 c
Copy the code

STEP 3

After modifying the file, save the changes with commit –amend

STEP 4

Git rebase –continue rebase

STEP 5

At this point, it is possible for other references to conflict, modify the conflicting sections before executing add and rebase –continue

conclusion

Git rebase and Git Reset are some of the best ways to undo a commit.

The resources

Modify the commit with rebase -i