git cancels the last commit or push

Table of contents

1. Cancel the last commit

Method 1: Use Git reset

Method 2: Use Git revert

Method 3: Use Git checkout

2. Cancel the last push

git push --force

git reflog

git reset

git push --force

3. Expansion

commit parameter

--mixed

--soft

--hard

--amend


1. Cancel the last commit

If you need to undo your last Git commit, there are a few different ways to do it. These include undoing a commit, committing to a new branch, rolling back with Git, and more.

Here are three methods:

Method 1: Use Git reset

Use the Git reset command to undo the last commit:

git reset HEAD~1

This moves the HEAD pointer back to the previous commit (HEAD~1) and clears the contents of the last commit.

or

git reset HEAD^

Withdraw twice or n times

git reset HEAD~2

Method 2: Use Git revert

Use the Git revert command to undo the last commit and create a new commit that undoes the original:

git revert HEAD

This creates a new commit that undoes the changes made before the commit. After entering this command on the command line, you need to edit the undone commit message so that Git creates a new commit.

Method 3: Use Git checkout

You can also use the Git checkout command to restore the working tree to the state of the last commit. This will wipe out all uncommitted changes, so make sure you have a backup:

git checkout HEAD~1

This will restore the working tree to the state of the last commit, which is the state before your last commit. Note that any modifications made here will be discarded.

Whichever method you use, make sure to back up your work first before undoing a commit. This helps you avoid accidentally deleting irrecoverable content during the operation.

2. Cancel the last push

If you have already pushed the code and want to withdraw this commit, you can do it through the following steps:

git push --force

First of all, when using git pushthe command, you need to add --forceparameters to force the existing commit on the remote warehouse to be overwritten. The command is as follows:

git push --force origin <branch_name>

Among them, <branch_name>represents the name of the branch you want to undo.

git reflog

If the previous status of the commit is not returned to the local warehouse, you need to use git reflogthe command to find the SHA-1 value of the commit. The command is as follows:

git reflog

This command will list the submission history of the entire Git repository, including the submission pointed to by the HEAD pointer and the submission that has been abandoned.

git reset

Find the SHA-1 value of a commit you want to return to, and then use the following command to return to the state of the commit:

git reset --hard <commit_SHA-1>

Among them, <commit_SHA-1>it indicates the SHA-1 value of the commit to return.

git push --force

Then use the previous push command to push, add --forceparameters, and overwrite the historical submission records of the remote warehouse. The command is as follows:

git push --force origin <branch_name>

Reminder: Using git push --forcecommands may cause irreversible effects on remote warehouses, other members' warehouses and historical versions, so you need to be cautious when operating. In general, a command is used in the workflow git revertto roll back a commit to ensure the integrity and maintainability of version control.

3. Expansion

git log view commit log

commit parameter

--mixed

 Change the code without deleting the workspace, undo the commit, and undo the git add . operation

 git reset --mixed HEAD^ is equivalent to git reset HEAD^

--soft

    Do not delete the workspace to change the code, undo the commit, and do not undo the git add.

--hard

    Delete the workspace to change the code, undo the commit, and undo the git add.

--amend

Modify the comment, enter the vim editor, and change: wq

Please like it if it is useful, and develop a good habit!

Please leave a message for encouragement, communication, and questions!

Guess you like

Origin blog.csdn.net/libusi001/article/details/131460699