Home Development Tools git How to cancel commit in git

How to cancel commit in git

Nov 29, 2021 pm 03:15 PM
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.

How to cancel commit in git

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 "" // 再次提交到仓库
Copy after login

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>
Copy after login

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>
Copy after login

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 // 强制推送到远端
Copy after login

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 值
Copy after login
  1. 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 use git 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
Copy after login

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
Copy after login

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  // 撤销这次提交
Copy after login

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
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to update code in git How to update code in git Apr 17, 2025 pm 04:45 PM

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

How to download git projects to local How to download git projects to local Apr 17, 2025 pm 04:36 PM

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

How to use git commit How to use git commit Apr 17, 2025 pm 03:57 PM

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

What to do if the git download is not active What to do if the git download is not active Apr 17, 2025 pm 04:54 PM

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

How to merge code in git How to merge code in git Apr 17, 2025 pm 04:39 PM

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 code in git How to update local code in git Apr 17, 2025 pm 04:48 PM

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/&lt;remote branch name&gt;. Resolve conflicts arising from mergers. Use git commit -m "Merge branch &lt;Remote branch name&gt;" to submit merge changes and apply updates.

How to solve the efficient search problem in PHP projects? Typesense helps you achieve it! How to solve the efficient search problem in PHP projects? Typesense helps you achieve it! Apr 17, 2025 pm 08:15 PM

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.

How to delete a repository by git How to delete a repository by git Apr 17, 2025 pm 04:03 PM

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.

See all articles