Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How git clears commit

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to remove commit from git". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "git how to clear commit"!

The methods of canceling commit by git are as follows: 1, revocation by "git rm" command; 2, revocation by "git reset" command; 3, revocation by "git rebase" command; 4, revocation by "git revert" command.

The operating environment of this tutorial: Windows7 system, Git2.30.0 version, Dell G3 computer.

Sometimes we submit the wrong code and need to undo the commit record of a certain time. Here are some ways to do this:

1. Delete files

If the commit you need to delete is one or more files, you can do the following.

1. A file submitted to the warehouse needs to be deleted. You can use the git rm command:

Git rm / / Delete a file from the workspace and staging area git commit-m "/ / submit it to the warehouse again

2. If you only want to delete files from the temporary storage area, and the local workspace does not change, you can:

Git rm-cached

3. If you accidentally delete a file in the workspace, you can use git checkout to overwrite the file in the staging area over the file in the workspace, thus restoring the deleted file:

Git checkout--

4. Delete the file with git rm, and record the deletion operation.

When you delete a file with rm, only the local physical file is deleted, and it is not removed from the git record.

5. Git add and git rm have similar functions

However, git add can only record the actions of adding and changing, and the action of deletion needs to be completed by git rm.

2. GitHub revokes a certain commit

If you need to delete not just a file, but interlaced code, there are three ways to delete commit.

1 、 git reset

Git reset: rollback to a commit.

Git reset-- soft: changes made after this submission will be returned to the staging area.

Git reset-- hard: no reservation is made for the changes made after this submission, and there is no record of git status viewing the workspace.

1) Roll back code

If the commit you need to delete is up-to-date, you can use the git reset command to roll back the code to the state of a previous commit, but be sure to back up the existing code, otherwise these changes will disappear after the rollback. The specific operations are as follows:

Git log / / query the commit_idgit reset to be rolled back-- hard commit_id / / HEAD will point to the submission record git push origin HEAD-- force / / force push to the remote end

2) erroneous deletion and restoration

If you find that you copied the wrong commit_id or deleted a commit record by mistake after rolling back the code, you can also restore it with the following code:

Git relog / / copy the previous hash value of the operation to be restored git reset-- hard hash / / replace hash with the hash value of the history to be restored

Note: when deleting an intermediate commit, it is best not to use git reset to roll back the remote library, because later when others submit the code, using git pull will also return their local warehouse to the previous version, which is prone to errors and increase unnecessary workload.

2 、 git rebase

Git rebase: use this command when two branches are not on the same line and you need to perform a merge operation.

1) cancel the submission

If an intermediate commit needs to be deleted, it can be done through the git rebase command as follows:

Git log / / find the previously submitted commit_idgit rebase-I commit_id / / replace commit_id with a copied value to enter Vim editing mode, and change the `pick` in front of the deleted commit to `drop` to save and exit Vim

That's it.

2) conflict resolution

Reabase conflicts are most likely to occur when this command is executed and can be resolved in the following ways:

Git diff / / View the content of the conflict / / manually resolve the conflict (the location of the conflict has been indicated in the file) git add or git add-A / / add git rebase-- continue / / continue rebase// if it is still in the rebase state, repeat 2, 3, 4 until the rebase completes and the word git push appears

3 、 git revert

Git revert: abandon a submission.

Previous submissions from git revert will remain in git log, and this revocation will be treated as a new submission.

Git revert-m: used for operations on merge nodes, and-m specifies a specific submission point.

1) cancel the submission

Using git revert is also a good choice when you want to undo an intermediate submission:

Git log / / find the commit_idgit revert commit_id that needs to be revoked / / cancel this submission

2) cancel the submission of the merge node

If this submission is a merge node, you need to add the-m directive:

Git revert commit_id-M1 / / the first submission point / / manually resolve the conflict git add-Agit commit-m "" git revert commit_id-m 2 / / the second submission point / / repeat 2Jing 3Jing 4git push so far, I believe you have a deeper understanding of "git how to clear commit", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report