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

AI Hentai Generator
Generate AI Hentai for free.

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



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.

Connecting a Git server to the public network includes five steps: 1. Set up the public IP address; 2. Open the firewall port (22, 9418, 80/443); 3. Configure SSH access (generate key pairs, create users); 4. Configure HTTP/HTTPS access (install servers, configure permissions); 5. Test the connection (using SSH client or Git commands).

Code conflict refers to a conflict that occurs when multiple developers modify the same piece of code and cause Git to merge without automatically selecting changes. The resolution steps include: Open the conflicting file and find out the conflicting code. Merge the code manually and copy the changes you want to keep into the conflict marker. Delete the conflict mark. Save and submit changes.

In order to securely connect to a remote Git server, an SSH key containing both public and private keys needs to be generated. The steps to generate an SSH key are as follows: Open the terminal and enter the command ssh-keygen -t rsa -b 4096. Select the key saving location. Enter a password phrase to protect the private key. Copy the public key to the remote server. Save the private key properly because it is the credentials for accessing the account.

To detect SSH through Git, you need to perform the following steps: Generate an SSH key pair. Add the public key to the Git server. Configure Git to use SSH. Test the SSH connection. Solve possible problems according to actual conditions.

How to add a public key to a Git account? Step: Generate an SSH key pair. Copy the public key. Add a public key in GitLab or GitHub. Test the SSH connection.

Creating a project using Git requires the following steps: 1. Install the official website of Git to download the corresponding version of Git and install it; 2. Initialize the project to create a repository using git init; 3. Add files to add files to the temporary storage area with git add; 4. Submit changes to commit and add instructions; 5. Push changes to push them with git push; 6. Pull changes to use git pull to get the latest changes from the remote repository using git pull.

Use git to submit code separately, providing granular change tracking and independent work ability. The steps are as follows: 1. Add the changed files; 2. Submit specific changes; 3. Repeat the above steps; 4. Push submission to the remote repository.
