Home > Development Tools > git > How to cancel commit in git

How to cancel commit in git

青灯夜游
Release: 2021-11-29 15:20:07
Original
217856 people have browsed it

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!

Related labels:
git
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template