Git is an open source distributed version control system that provides developers with some important commands and tools, which is extremely convenient during software development and collaboration. Among them, the key to Git communication is the concept of commit, because during the submission process, the team can learn about the code modifications and personal contributions.
However, sometimes we need to delete submitted commits. For example, if some useless code or sensitive information is submitted, it needs to be deleted from the remote code base. At this time, we need to learn how to delete submitted commits in Git, and how to push the deleted changes to the remote code base. The following will introduce how to delete remote commits in Git.
1. The essence of Git commit
In Git, each commit has a unique SHA-1 hash value as an identifier. This hash value is calculated by Git based on the contents of the commit. If any character changes, the hash value will change accordingly. This is why a commit can only be deleted, not modified.
2. Use the Git reset command to delete a commit
To delete a commit, you first need to use the Git reset command to point the HEAD pointer to the previous commit that needs to be deleted. This HEAD pointer points to the latest commit of the local code base. The reset command can be used to modify the position of the HEAD pointer. If you want to delete a commit, you need to point HEAD to the previous commit of the commit that needs to be deleted, so that you can "remove" the commit that needs to be deleted.
For example, we have the following commit records:
commit2 commit1
If we want to delete commit2, we need to point HEAD to commit1 first:
git reset HEAD~1
This command means to change HEAD Pointing to the previous commit (commit1), this command not only moves the HEAD pointer to commit1, but also deletes commit2 from the Git local code base.
3. Use the Git push command to submit changes to the remote code repository
In the previous step, use the reset command to delete the commit in the local code repository, but if you want to delete the commit in the remote code repository commit, you need to push the deleted changes to the remote code base. Here are two methods:
1. Force push: In Git, force push is the most commonly used method because it allows the remote code base to be updated immediately. The command to force push is:
git push -f
This command means to force local changes to the remote code base, even if these changes will overwrite the committed commits in the remote code base.
2. Use "revert" to reverse commit: This method is suitable for situations where you do not want to delete the commit, but reverse it to the opposite result. To use this method, you need to first submit a "revert" commit, which cancels the previous commit and adds a corresponding reversal commit. The contents of this reversal commit are the reverse of the changes made by the previous commit, thus restoring the code base to its previous state. Example of this command:
git revert <commit-id>
4. Precautions
You should be careful when deleting remote commits, because once deleted, they cannot be restored. Make sure you have backed up the code of the commit that needs to be deleted so that you can restore it if needed in the future.
You cannot delete the code from the public repository, because after deleting the commit, it still exists in other people's local repositories, and the public repository will not be cleared. If you want to clear sensitive information, consider using Git's git filter-branch command or a similar tool.
Summary:
The above is how Git deletes remote commits. Developers who use Git commands for code management need to understand these basic principles and operating procedures. When deleting a commit, you need to pay attention to protecting the source code in the code base and make a backup to avoid data loss. At the same time, it is recommended to remove sensitive information from the code to protect the security of the project.
The above is the detailed content of Summary of Git's method of deleting remote commits. For more information, please follow other related articles on the PHP Chinese website!