How to cancel commit in git
How to cancel commit in git: 1. Use the "git rm" command to undo; 2. Use the "git reset" command to undo; 3. Use the "git rebase" command to undo; 4. Use the "git revert" command Undo.

The operating environment of this tutorial: Windows 7 system, Git version 2.30.0, Dell G3 computer.
Sometimes we submit the wrong code and need to revoke a certain commit record. Here are several methods:
1. Delete the file
If the commit that needs to be deleted is one or more files, you can perform the following operations.
1. If a file submitted to the warehouse needs to be deleted, you can use the git rm command:
git rm <file> // 从工作区和暂存区删除某个文件 git commit -m "" // 再次提交到仓库
2. If you only want to delete the file from the temporary storage area, If you do not make changes to the local workspace, you can:
git rm --cached <file>
3. If you accidentally delete a file in the workspace, you can use git checkout to overwrite the file in the temporary storage area. files in the area to recover accidentally deleted files:
git checkout -- <file>
4. Use git rm to delete files, and the deletion operation will also be recorded;
Use rm Deleting files only deletes local physical files and does not remove them from git records.
5, git add and git rm have similar functions,
but git add can only record additions, Change actions and deletion actions need to be completed by git rm.
2. GitHub Undoes a Commit
If you need to delete not just a file, but interleaved code, then there are the following There are three ways to delete commits.
1. git reset
-
git reset: Roll back to a certain commit. -
git reset --soft: Modifications after this submission will be returned to the staging area. -
git reset --hard: No modifications will be retained after this submission.git statusThere is no record when viewing the workspace.
1) Rollback code
If the commit that needs to be deleted is the latest, you can roll back the code to a previous commit through the git reset command status, but be sure to back up the existing code, otherwise these changes will disappear after rollback. The specific operations are as follows:
git log // 查询要回滚的 commit_id git reset --hard commit_id // HEAD 就会指向此次的提交记录 git push origin HEAD --force // 强制推送到远端
2) Accidental deletion recovery
If you find that you copied the wrong commit_id after rolling back the code, or deleted a commit record by mistake, you can also restore it through the following code:
git relog // 复制要恢复操作的前面的 hash 值 git reset --hard hash // 将 hash 换成要恢复的历史记录的 hash 值
- Note: It is best not to use
git resetto roll back the remote library when deleting a certain commit in the middle, because later others will usegit pullwhen submitting code. It will also roll back its local warehouse to the previous version, which is prone to errors and increases unnecessary workload.
2. git rebase
-
git rebase: When the two branches are not on the same line, a merge operation needs to be performed Use this command.
1) Undo the commit
If a certain commit in the middle needs to be deleted, you can use the git rebase command. The method is as follows:
git log // 查找要删除的前一次提交的 commit_id git rebase -i commit_id // 将 commit_id 替换成复制的值 进入 Vim 编辑模式,将要删除的 commit 前面的 `pick` 改成 `drop` 保存并退出 Vim
This is done.
2) Resolve conflicts
It is very likely that a rebase conflict will occur when this command is executed, which can be resolved by the following methods:
git diff // 查看冲突内容 // 手动解决冲突(冲突位置已在文件中标明) git add <file> 或 git add -A // 添加 git rebase --continue // 继续 rebase // 若还在 rebase 状态,则重复 2、3、4,直至 rebase 完成出现 applying 字样 git push
3, git revert
-
git revert: Abandon a commit.git revertThe previous submission will still remain in the git log, and this revocation will be treated as a new submission. -
git revert -m: used to operate the merge node, -m specifies a specific submission point.
1) Undo a commit
When you want to undo a commit in the middle, using git revert is also a good choice:
git log // 查找需要撤销的 commit_id git revert commit_id // 撤销这次提交
2) Undo the merge node submission
If this submission is a merge node, you need to add the -m command:
git revert commit_id -m 1 // 第一个提交点 // 手动解决冲突 git add -A git commit -m "" git revert commit_id -m 2 // 第二个提交点 // 重复 2,3,4 git push
Recommended learning: "Git Tutorial》
The above is the detailed content of How to cancel commit in git. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1793
16
1736
56
1587
29
267
587
How to install numpy library in python three ways to install numpy library in python
May 28, 2025 pm 04:03 PM
There are three ways to install the NumPy library: 1. Use pip to install: pipinstallnumpy, which is simple but may encounter permissions or network problems; 2. Use conda to install: condainstallnumpy, which is suitable for Anaconda environment, and automatically resolves dependencies; 3. Install: gitclone from source code and compile, which is suitable for special needs but complicated processes.
Performance Tuning of Jenkins Deployment on Debian
May 28, 2025 pm 04:51 PM
Deploying and tuning Jenkins on Debian is a process involving multiple steps, including installation, configuration, plug-in management, and performance optimization. Here is a detailed guide to help you achieve efficient Jenkins deployment. Installing Jenkins First, make sure your system has a Java environment installed. Jenkins requires a Java runtime environment (JRE) to run properly. sudoaptupdatesudoaptininstallopenjdk-11-jdk Verify that Java installation is successful: java-version Next, add J
Create and manage multiple project workspaces in VSCode
May 29, 2025 pm 10:09 PM
Create and manage multiple project workspaces in VSCode through the following steps: 1. Click the "Manage" button in the lower left corner, select "New Workspace", and decide the save location. 2. Give the workspace a meaningful name, such as "WebDev" or "Backend". 3. Switch the project in Explorer. 4. Use the .code-workspace file to configure multiple projects and settings. 5. Pay attention to version control and dependency management to ensure that each project has .gitignore and package.json files. 6. Clean useless files regularly and consider using remote development skills
Solve the layout settings and display problems of VSCode in multi-screen environment
May 29, 2025 pm 10:12 PM
Using VSCode in a multi-screen environment can solve layout and display problems by adjusting the window size and position, setting workspaces, adjusting interface scaling, rationally laying tool windows, updating software and extensions, optimizing performance, and saving layout configuration, thereby improving development efficiency.
How to create Laravel package (Package) development?
May 29, 2025 pm 09:12 PM
The steps to create a package in Laravel include: 1) Understanding the advantages of packages, such as modularity and reuse; 2) following Laravel naming and structural specifications; 3) creating a service provider using artisan command; 4) publishing configuration files correctly; 5) managing version control and publishing to Packagist; 6) performing rigorous testing; 7) writing detailed documentation; 8) ensuring compatibility with different Laravel versions.
Analysis of VSCode's support trends and related issues for emerging programming languages
May 29, 2025 pm 10:06 PM
VSCode's support trend for emerging programming languages is positive, mainly reflected in syntax highlighting, intelligent code completion, debugging support and version control integration. Despite scaling quality and performance issues, they can be addressed by choosing high-quality scaling, optimizing configurations, and actively participating in community contributions.
Troubleshooting data consistency issues when PHP operates MySQL database
May 28, 2025 pm 06:12 PM
To troubleshoot data consistency issues when PHP operates MySQL databases, you need to start with transaction management, code logic, and database configuration. 1. Use STARTTRANSACTION and COMMIT/ROLLBACK to ensure transaction integrity. 2. Check the code logic to avoid variable errors. 3. Set appropriate MySQL isolation level such as REPEATABLEREAD. 4. Use ORM tools to simplify transaction management. 5. Check PHP and MySQL log location issues. 6. Use the version control system to manage database change scripts.
The reasons and solutions for editor crash after VSCode plug-in update
May 29, 2025 pm 10:03 PM
The reason why the editor crashes after the VSCode plugin is updated is that there is compatibility issues with the plugin with existing versions of VSCode or other plugins. Solutions include: 1. Disable the plug-in to troubleshoot problems one by one; 2. Downgrade the problem plug-in to the previous version; 3. Find alternative plug-ins; 4. Keep VSCode and plug-in updated and conduct sufficient testing; 5. Set up automatic backup function to prevent data loss.


