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 status
There 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 reset
to roll back the remote library when deleting a certain commit in the middle, because later others will usegit pull
when 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 revert
The 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

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

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

Steps to update git code: Check out code: git clone https://github.com/username/repo.git Get the latest changes: git fetch merge changes: git merge origin/master push changes (optional): git push origin master

To download projects locally via Git, follow these steps: Install Git. Navigate to the project directory. cloning the remote repository using the following command: git clone https://github.com/username/repository-name.git

Git Commit is a command that records file changes to a Git repository to save a snapshot of the current state of the project. How to use it is as follows: Add changes to the temporary storage area Write a concise and informative submission message to save and exit the submission message to complete the submission optionally: Add a signature for the submission Use git log to view the submission content

Resolve: When Git download speed is slow, you can take the following steps: Check the network connection and try to switch the connection method. Optimize Git configuration: Increase the POST buffer size (git config --global http.postBuffer 524288000), and reduce the low-speed limit (git config --global http.lowSpeedLimit 1000). Use a Git proxy (such as git-proxy or git-lfs-proxy). Try using a different Git client (such as Sourcetree or Github Desktop). Check for fire protection

Git code merge process: Pull the latest changes to avoid conflicts. Switch to the branch you want to merge. Initiate a merge, specifying the branch to merge. Resolve merge conflicts (if any). Staging and commit merge, providing commit message.

How to update local Git code? Use git fetch to pull the latest changes from the remote repository. Merge remote changes to the local branch using git merge origin/<remote branch name>. Resolve conflicts arising from mergers. Use git commit -m "Merge branch <Remote branch name>" to submit merge changes and apply updates.

When developing an e-commerce website, I encountered a difficult problem: How to achieve efficient search functions in large amounts of product data? Traditional database searches are inefficient and have poor user experience. After some research, I discovered the search engine Typesense and solved this problem through its official PHP client typesense/typesense-php, which greatly improved the search performance.

To delete a Git repository, follow these steps: Confirm the repository you want to delete. Local deletion of repository: Use the rm -rf command to delete its folder. Remotely delete a warehouse: Navigate to the warehouse settings, find the "Delete Warehouse" option, and confirm the operation.
